[英]Check box using jQuery
JSON data looks like this: JSON数据如下所示:
{"id":8,"userName":"gentelman","fullname":"Alexen","email":"alex@yahoo.com",
"mobile":"0060111434325","designation":null,"add_content":1,"edit_content":1,
"delete_content":1,"manage_user":1,"view_log":1}
I am trying to make checkbox checked
when add_content = 1
, else make it unchecked
. 我想在add_content = 1
时checked
复选框,否则unchecked
它。 Now it works only when I refresh page. 现在它只在我刷新页面时才有效。 I want to make it work without refreshing page. 我想让它在没有刷新页面的情况下工作。 How to do that? 怎么做?
load: function (id) {
var self = this;
KeyWord.byId(
id,
function (data) {
if (data.add_content == 1) {
$("#add_content").attr('checked', true);
} else {
$("#add_content").attr('checked', false);
}
self.formHandler.load(data);
},
function (error) { },
function () { }
);
}
您可以在一行中完成,只需确保使用.prop()
$("#add_content").prop('checked', data.add_content == 1);
Change this code : 更改此代码:
if (data.add_content == 1) {
$("#add_content").prop('checked', true);
} else {
$("#add_content").prop('checked', false);
}
self.formHandler.load(data);
To this : 对此:
if (data.add_content == 1) {
$("#add_content").attr("checked", "checked").closet(span).addClass("checked");
}
self.formHandler.load(data);
yes you need to use prop() Setting a boolean attribute with jquery 是的,您需要使用prop() 使用jquery设置布尔属性
http://api.jquery.com/prop/ http://api.jquery.com/prop/
load: function (id) {
var self = this;
KeyWord.byId(
id,
function (data) {
if (data.add_content == 1) {
$("#add_content").prop('checked', true);
} else {
$("#add_content").prop('checked', false);
}
self.formHandler.load(data);
},
function (error) { },
function () { }
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.