已選中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;
    });     
});

#1樓

參考:https://stackoom.com/question/117ie/已選中jquery-set複選框


#2樓

Dude try below code : 老兄嘗試下面的代碼:

$("div.row-form input[type='checkbox']").attr('checked','checked')

OR 要麼

$("div.row-form #estado_cat").attr("checked","checked");

OR 要麼

$("div.row-form #estado_cat").attr("checked",true);

#3樓

Try this since your are using jQuery UI probably (if not please comment) 試試這個,因爲你可能正在使用jQuery UI(如果沒有請註釋)

 $("#fModal" ).dialog({
     open: function( event, ui ) {

     if(//some hidden value check which stores the DB value==expected value for
      checking the Checkbox)

         $("div.row-form input[type='checkbox']").attr('checked','checked');

    }
   });

#4樓

Since you are in a modal window (whether dynamic or in the page) you can use the following code to accomplish your goal: (I ran into the same problem on my site and this is how I fixed it) 由於您處於模態窗口(無論是動態的還是在頁面中),您可以使用以下代碼來實現您的目標:(我在我的網站上遇到了同樣的問題,這就是我修復它的方法)

HTML: HTML:

<div class="content-container" style="text-align: right;">
    <input type="checkbox" id="QueryGroupCopyQueries">
    <label for="QueryGroupCopyQueries">Create Copies</label>                                                   
</div>

CODE: 碼:

$.each(queriesToAddToGroup, function (index, query) {
    if (query.groupAddType === queriesGroupAddType.COPY) {

        // USE FIND against your dynamic window and PROP to set the value
        // if you are using JQUERY 1.6 or higher.
        $(kendoWindow).find("input#QueryGroupCopyQueries").prop("checked", true);

        return;
    }

In the above "kendoWindow" is my window (mine is dynamic, but I also do this when appending to an element directly in the page). 在上面的“kendoWindow”是我的窗口(我的是動態的,但是當我直接在頁面中附加元素時也會這樣做)。 I am looping through an array of data ("queriesToAddToGroup") to see if any rows have an attribute of COPY. 我循環遍歷數據數組(“queriesToAddToGroup”)以查看是否有任何行具有COPY屬性。 If so I want to turn on the checkbox within the window that sets that attribute when a user saves from this window. 如果是這樣,我想打開窗口中的複選框,該複選框在用戶從此窗口保存時設置該屬性。


#5樓

You can write like below given code. 你可以寫下面給出的代碼。

HTML HTML

<input type="checkbox"  id="estado_cat" class="ibtn">

jQuery jQuery的

$(document).ready(function(){
    $(".ibtn").click(function(){
        $(".ibtn").attr("checked", "checked");
    });
});

Hope it work for you. 希望它對你有用。


#6樓

您是否嘗試在$("#estado_cat").checkboxradio("refresh")運行$("#estado_cat").checkboxradio("refresh")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章