我有这些复选框,我已经可以从它的表中获取值,但我只是不知道它最终会如何选中那些带有已经插入到我的数据库中的值的复选框。
function editPageLink(id){
$.ajax({
type : 'GET',
async : false,
data : {
key : id
},
url : baseUrl+'/admin/getdiscountinfo',
error: function(req,error){
notify('e',req.statusText);
},
dataType: 'json',
cache: false,
success : function(msg){
if(Object.keys(msg).length != 0){
newPage();
saveType = 'u';
key = msg[0].key;
$('#discountCode').val(msg[0].discountCode);
$('#discountName').val(msg[0].discountName);
$('#discountDesc').val(msg[0].discountDesc);
$('#discountType').val(msg[0].discountType);
$('#expiration').val(msg[0].expiration);
$('#expStartDate').val(msg[0].expStartDate);
$('#expEndDate').val(msg[0].expEndDate);
$('#discountValue').val(msg[0].discountValue);
$('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
$('.chkbox2').check(checked); // i dunno if this is right.
};
$('#discounteTransaction').val(msg[0].discountTransaction);
}
}
});
}
我试过了,但仍然不起作用。 感谢您的帮助! :D
编辑:我的#productId
和'.chkbox2'
是一样的。 这部分,获取值是否正确?
$('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
$('.chkbox2').check(checked);
};
$('.chkbox2').prop('checked', true);
使用此选项选中复选框
$('.chkbox2').prop('checked', true);
上面的代码会将复选框设置为选中
$('.chkbox2').prop('checked', false);
上面的代码将取消选中复选框
$('.chkbox2').check(checked); // i dunno if this is right.
在jQuery中选中复选框的标准方法是使用.prop()
:
$('.chkbox2').prop('checked', bool);
如果 bool
= true,则将选中该复选框。(或者,如果bool
为假,则取消选中(
您还可以通过简单地省略第二个参数来检索当前已检查状态:
$('.chkbox2').prop('checked'); // Returns true or false.
此外,在 if 条件下,您有一个错误:
if ('.chkbox2'.val() == true){
应该是
if( $('.chkbox2').val() == true )
因为你缺少选择器的jQuery包装器。
啊! 终于解决了,我只是改变了
$('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
$('.chkbox2').check(checked); // i dunno if this is right.
};
自
$('.chkbox2').val(msg[0].productId,':checked',true);
是的,奥霍拉特! 哈哈,感谢您的评论,我获得了:D的想法
尝试将其添加到您的代码中:D
$('.chkbox2').val(msg[0].productId,':checked',true);
$('#checkbox'(.attr('checked'(
$('#checkbox'(.prop('checked'(//true
$('#checkbox'(.is(':checked'(//true
$('#checkbox'([0].check//true