簡體   English   中英

復選框-僅選中一個復選框就選中多個復選框

[英]Checkbox - Getting multiple check boxes checked on checking only one

我正在嘗試實現一個復選框,該復選框選中頁面上的所有復選框。 但是,當我稍微更改代碼時,它停止工作。 我想保留更改后的代碼,並且仍然希望該功能正常運行。 請幫助! *請忽略丟失的標簽(如果有)。 除非我在編輯問題時犯了錯誤,否則它是正確的。

   <script>     
    function toggle(source) {
        checkboxes = document.getElementsByName('qchecked[]');
        for(var i=0, n=checkboxes.length;i<n;i++){
            checkboxes[i].checked = source.checked;
        }
    }
</script>
<html> 
/* Code for the check box which when checked, checks all the checkbox. */
<input type='checkbox' class='css-checkbox' id='checkbox' onClick='toggle(this)'/>
<label for='checkbox' class='css-label lite-y-green'></label>



    /* Code for the check boxes which should be checked when the check box with id=checkbox is checked. 
I changed the code from INITIAL CODE to CHANGED CODE for some other purpose and the toggle stopped working. 
Now clicking on that one check box is not marking or un marking all the check boxes. */

    <!-- INITIAL CODE -->
    <input type='checkbox' id='yes_checkbox[$index]' class='css-checkbox' name='qchecked[]'/>
    <label for='yes_checkbox[$index]' class='css-label lite-y-green'></label>

    <!-- CHANGED CODE -->
    <input type='checkbox' id='yes_checkbox[$index]' class='css-checkbox' name='qchecked[$array6[qID]]'/>
    <label for='yes_checkbox[$index]' class='css-label lite-y-green'></label>
    </html>

給所有元素一個類而不是名稱,應該使用

getElementsByClassName('your_class');

由於您inputs name不同,因此可以使用common class

checkboxes = document.getElementsByClassName('css-checkbox');

嘗試這個..

<ul class="chk-container">
<li><input type="checkbox" id="selecctall"/> Selecct All</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item1"> This is Item 1</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item2"> This is Item 2</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item3"> This is Item 3</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item4"> This is Item 4</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item5"> This is Item 5</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item6"> This is Item 6</li>
<li><input class="checkbox2" type="checkbox" name="check[]" value="item6"> Do not select this</li>
</ul>

$(document).ready(function() {
    $('#selecctall').click(function(event) {  //on click 
        if(this.checked) { // check select status
            $('.checkbox1').each(function() { //loop through each checkbox
                this.checked = true;  //select all checkboxes with class "checkbox1"               
            });
        }else{
            $('.checkbox1').each(function() { //loop through each checkbox
                this.checked = false; //deselect all checkboxes with class "checkbox1"                       
            });         
        }
    });

});

演示: https : //jsfiddle.net/go1gL743/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM