jquery集复选框 [英] jquery set checkbox checked
问题描述
我已经尝试了所有可能的方法,但我还是没有得到它的工作。
我有一个带有复选框的模态窗口
我想当模态打开时,复选框
取消选中应该基于数据库值。
I already tried all the possible ways, but I still didn't get it working.
I have a modal window with a checkbox
I want that when the modal opens, the checkbox
check or uncheck should be based on a database value. (I have that already working with others form fields.) I started trying to get it checked but it didn't work.
我的html div:
My html div:
<div id="fModal" class="modal" >
...
<div class="row-form">
<div class="span12">
<span class="top title">Estado</span>
<input type="checkbox" id="estado_cat" class="ibtn">
</div>
</div>
</div>
和jquery:
$("#estado_cat").prop( "checked", true );
我也尝试过 attr
看到这里在论坛,但没有似乎工作。
有人能给我正确的方式吗?
I also tried with attr
, and others seen here in the forums, but none seem to work.
Can someone point me the right way?
编辑:
确定,我真的在这里缺少一些东西...我可以检查/使用代码,如果复选框是在页面中,但它是在模态窗口,我不能。我尝试了几十种不同的方式...
ok, I'm really missing something here... I can check/uncheck using code if the check box is in the page, but is it's in the modal window, I can't. I tried dozens of different ways...
我有一个应该打开模态的链接:
I have a link that's supposed to open the modal:
和jquery收听点击并执行一些操作,如用来自数据库的数据填充一些文本框。一切都像我想要的,但问题是,我不能设置复选框选中/取消选中使用代码。帮助请!
and jquery to "listen" the click and execute some operations like filling some text boxes with data coming from database. Everything works like I want but the problem is that I can't set checkbox checked/unchecked using code. help please!
$(function() {
$(".editButton").click(function(){
var id = $(this).data('id');
$.ajax({
type: "POST",
url: "process.php",
dataType:"json",
data: { id: id, op: "edit" },
}).done(function( data ) {
//the next two lines work fine, i.e., it grabs the value from database and fills the textboxes
$("#nome_categoria").val( data['nome_categoria'] );
$("#descricao_categoria").val( data['descricao_categoria'] );
//then I tried to set the checkbox checked (because its unchecked by default) and it does not work
$("#estado_cat").prop("checked", true);
$('#fModal').modal('show');
});
evt.preventDefault();
return false;
});
});
推荐答案
/ p>
you have to use the 'prop' function :
.prop('checked', true);
在jQuery 1.6 之前(请参阅 user2063626的回答):
.attr('checked','checked')
这篇关于jquery集复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!