jQuery 设置复选框已选中 [英] jQuery set checkbox checked
问题描述
我已经尝试了所有可能的方法,但仍然无法正常工作.我有一个带有 checkbox
的模态窗口,我希望当模态打开时,checkbox
选中或取消选中应该基于数据库值.(我已经与其他表单字段一起使用了.)我开始尝试检查它,但它没有用.
我的 HTML div:
和 jQuery:
$("#estado_cat").prop( "checked", true );
我也尝试过使用 attr
以及在论坛中看到的其他人,但似乎都不起作用.
有人可以指出我正确的方法吗?
编辑
好的,我真的错过了一些东西.如果复选框在页面中,我可以使用代码选中/取消选中,但它是否在 modal 窗口中,我不能.我尝试了几十种不同的方法.
我有一个应该打开模态的链接:
<a href='#' data-id='".$row['id_cat']."'class='editButton icon-pencil'></a>
和 jQuery 来聆听"单击并执行一些操作,例如用来自数据库的数据填充一些文本框.一切都像我想要的那样工作,但问题是我无法使用代码设置复选框选中/取消选中.请帮忙!
$(function () {$(".editButton").click(function () {var id = $(this).data('id');$.ajax({类型:POST",网址:process.php",数据类型:json",数据: {身份证:身份证,操作:编辑"},}).完成(功能(数据){//接下来的两行工作正常,//即它从数据库中获取值并填充文本框$("#nome_categoria").val(data['nome_categoria']);$("#descricao_categoria").val(data['descricao_categoria']);//然后我尝试将复选框设置为选中(因为默认情况下它是未选中的)//它不起作用$("#estado_cat").prop("checked", true);$('#fModal').modal('show');});evt.preventDefault();返回假;});});
你必须使用'prop'函数:
.prop('checked', true);
jQuery 1.6 之前(参见user2063626的答案):
.attr('checked','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.
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>
and the jQuery:
$("#estado_cat").prop( "checked", true );
I also tried with attr
, and others seen here in the forums, but none seem to work.
Can someone point me the right way?
EDIT
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:
<a href='#' data-id='".$row['id_cat']."' class='editButton icon-pencil'></a>
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 it's unchecked by default)
// and it does not work
$("#estado_cat").prop("checked", true);
$('#fModal').modal('show');
});
evt.preventDefault();
return false;
});
});
you have to use the 'prop' function :
.prop('checked', true);
Before jQuery 1.6 (see user2063626's answer):
.attr('checked','checked')
这篇关于jQuery 设置复选框已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!