简体   繁体   中英

Select multiple checkbox with same name

For example I have 5 checkboxes and all of them have same name. I check checkboxes, submit and then delete checkboxes I checked. Here I don´t have any problems, the problem it´s check for example the number 1 , 3 , 5 , etc , but has the same name and when check one uncheck other

How it´s possible check all checbox i want from for example 5 checboxes

For example :

Check 1 - checked
Check 2 - unchecked
Check 3 - checked
Check 4 - unchecked
Check 5 - checked

Thank´s Regards

If you really can't give them different names or id and you want to check them all :

$('input[name="thename"]').prop('checked', true);

If you want to check the first, the third and the fifth checkbox, you can do this :

$('input[name="thename"]').filter(':eq(0), :eq(2), :eq(4)').prop('checked', true);

use id's to identify them individually:

<input type='checkbox' name='name' id='cb_1'/>
<input type='checkbox' name='name' id='cb_2'/>
<input type='checkbox' name='name' id='cb_3'/>
<input type='checkbox' name='name' id='cb_4'/>
<input type='checkbox' name='name' id='cb_5'/>

If you want to delete checkbox you check then this could help if you use jQuery

1 <input type='checkbox' name='my_name' /><br>
2 <input type='checkbox' name='my_name' /><br>
3 <input type='checkbox' name='my_name' /><br>
4 <input type='checkbox' name='my_name' /><br>
5 <input type='checkbox' name='my_name' /><br>
    <script type="text/javascript">​
        // Event handler activates when value of input with specified name is changed
        $("input[name=my_name]").on("change", function(event) {
            // checks if checkbox was checked, if so - deletes it
            if (true === $(this).prop('checked')) $(this).remove();
        });​
    </script>

http://jsfiddle.net/56nBw/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM