Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

jquery - 已选中jquery set复选框(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.

(我有一个模态窗口,我想要一个checkbox ,当模态打开时, checkbox检查或取消选中应该基于数据库值。)

(I have that already working with others form fields.) I started trying to get it checked but it didn't work.

((我已经与其他表单字段合作了。)我开始尝试检查它但是它不起作用。)

My html div:

(我的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:

(和jquery:)

$("#estado_cat").prop( "checked", true );

I also tried with attr , and others seen here in the forums, but none seem to work.

(我也试过attr ,还有其他人在论坛上看过,但似乎都没有用。)

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:

(我有一个应该打开模态的链接:)

and jquery to "listen" the click and execute some operations like filling some text boxes with data coming from database.

(和jquery“监听”点击并执行一些操作,比如用来自数据库的数据填充一些文本框。)

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;
    });     
});
  ask by psopa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you have to use the 'prop' function :

(你必须使用'prop'功能:)

.prop('checked', true);

Before jQuery 1.6 (see user2063626's answer ):

(在jQuery 1.6之前 (参见user2063626的回答 ):)

.attr('checked','checked')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...