Kendo 可观察复选框未通过 jQuery .prop("checked") 更新
Kendo observable checkbox not updating through jQuery .prop("checked")
我有一个绑定到多个复选框的 Kendo 可观察对象。当我通过单击它们来更新复选框时,可观察对象会正确更新。但是,如果我通过代码更新检查状态,则可观察对象不会更新。
下面是一个示例片段,我有一个 Kendo UI Dojo。单击红色、绿色或蓝色复选框时,正确的信息会显示在框中;但是,如果您使用 "Select All" 选项,则不会更新可观察对象。
如何正确编写 "Select All" 复选框的代码,以便可观察对象随选中状态一起更新?
$(document).ready(function () {
var colordata = null;
colordata = kendo.observable({
colors: []
});
kendo.bind($("#chkbox-options"), colordata);
colordata.bind("change", function(e) {
var selectedColors = '';
$.each(colordata.colors, function(key, value){
selectedColors = selectedColors + " " + value;
});
if(colordata.colors.length == 0){
$("#selected").val('no colors selected');
}else{
$("#selected").val('selected colors:' + selectedColors);
}
});
$("#all").on("click", function() {
$("#chkbox-options input[type='checkbox']").prop("checked", $(this).prop("checked"));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2015.1.318/js/kendo.all.min.js"></script>
<input id="all" type="checkbox" />
<label for="all">Select All</label>
<div class="k-group" id="chkbox-options">
<label for="chk1">Red</label>
<input type="checkbox" id="chk1" value="Red" data-bind="checked: colors" />
<label for="chk2">Green</label>
<input type="checkbox" id="chk2" value="Green" data-bind="checked: colors" />
<label for="chk3">Blue</label>
<input type="checkbox" id="chk3" value="Blue" data-bind="checked: colors" />
</div>
<input id="selected" style="width:100%;" />
你的错误是你正在使用 属性 更改,jquery 道具方法不会触发颜色数据更改事件,jQuery API 解释:
For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus
因此,您应该使用 click()
方法,而不是使用 prop()
。 fixed dojo 1
您可以在使用 prop()
后使用 trigger("change")
的另一种方法。 fixed dojo 2
我有一个绑定到多个复选框的 Kendo 可观察对象。当我通过单击它们来更新复选框时,可观察对象会正确更新。但是,如果我通过代码更新检查状态,则可观察对象不会更新。
下面是一个示例片段,我有一个 Kendo UI Dojo。单击红色、绿色或蓝色复选框时,正确的信息会显示在框中;但是,如果您使用 "Select All" 选项,则不会更新可观察对象。
如何正确编写 "Select All" 复选框的代码,以便可观察对象随选中状态一起更新?
$(document).ready(function () {
var colordata = null;
colordata = kendo.observable({
colors: []
});
kendo.bind($("#chkbox-options"), colordata);
colordata.bind("change", function(e) {
var selectedColors = '';
$.each(colordata.colors, function(key, value){
selectedColors = selectedColors + " " + value;
});
if(colordata.colors.length == 0){
$("#selected").val('no colors selected');
}else{
$("#selected").val('selected colors:' + selectedColors);
}
});
$("#all").on("click", function() {
$("#chkbox-options input[type='checkbox']").prop("checked", $(this).prop("checked"));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2015.1.318/js/kendo.all.min.js"></script>
<input id="all" type="checkbox" />
<label for="all">Select All</label>
<div class="k-group" id="chkbox-options">
<label for="chk1">Red</label>
<input type="checkbox" id="chk1" value="Red" data-bind="checked: colors" />
<label for="chk2">Green</label>
<input type="checkbox" id="chk2" value="Green" data-bind="checked: colors" />
<label for="chk3">Blue</label>
<input type="checkbox" id="chk3" value="Blue" data-bind="checked: colors" />
</div>
<input id="selected" style="width:100%;" />
你的错误是你正在使用 属性 更改,jquery 道具方法不会触发颜色数据更改事件,jQuery API 解释:
For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus
因此,您应该使用 click()
方法,而不是使用 prop()
。 fixed dojo 1
您可以在使用 prop()
后使用 trigger("change")
的另一种方法。 fixed dojo 2