[英]jQuery checking and unchecking checkboxes
我必須選中並取消選中復選框。 但是它不能很好地工作。
示例:當第一負載的狀態為活動時:
示例:在第一個負載上狀態為非活動狀態:
這是我的示例代碼:
var statusCheck = $('.status-box');
$.ajax({
url: 'sample/url',
type: 'POST',
data: {
user_id: ids,
status: statusVal
},
success: function (response) {
if (response == 'ok') {
$.each(statusCheck, function () {
var _this = $(this);
if (_this.is(':checked')) {
_this.removeAttr('checked');
} else {
_this.attr('checked', '');
}
});
}
}
});
var statusCheck = $('.status-box')
$.ajax({
url : 'sample/url',
type : 'POST',
data : { user_id: ids, status: statusVal },
success : function( response ) {
if( response == 'ok' ) {
$.each(statusCheck, function(){
var _this = $(this);
if( _this.is(':checked') ) {
_this.removeAttr('checked');
} else {
_this.attr('checked', 'checked');
// ...............^ here ^
}
});
}
}
});
試試這個: _this.attr('checked', 'checked');
同樣,此鏈接中的 .prop()
可能會.prop()
神奇作用;)
我在使用JQuery選中和取消選中復選框時遇到了問題(奇怪的異常)。 我正在使用.attr
和.removeAttr
,當我將其更改為.prop('checked',false/true)
我解決了該問題。
在jQuery 1.6之前,.attr()方法在檢索某些屬性時有時會考慮屬性值,這可能會導致行為不一致。 從jQuery 1.6開始,.prop()方法提供了一種顯式檢索屬性值的方法,而.attr()則檢索屬性。 參考:
注意您應該使用JQuery 1.6 +
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.