简体   繁体   中英

How to select radio button using button with jquery

So basically im creating an online voting system, i used radio buttons as identifier of which candidate is being voted. By the way, candidates are from my database.
This is the html code:

        <td align="center">
        <input type="hidden" id="myhidden" value="Independent">
        <button class="ind">Independent</button></td>
        <td align="center">
        <input type="hidden" id="myhidden2" value="Liberal Partylist">
        <button class="lp">Liberal Partylist</button></td>
        <td align="center">
        <input type="hidden" id="myhidden3" value="UNO">
        <button class="uno">UNO</button></td>

This is my jquery code:

 $(document).ready(function (){
    $('.ind').click(function() {
    $('input[id="' + $("#myhidden").val() + '"]').attr('checked', 'checked');
    });

    $('.lp').click(function() {
    $('input[id="' + $("#myhidden2").val() + '"]').attr('checked', 'checked');
    });

    $('.uno').click(function() {
    $('input[id="' + $("#myhidden3").val() + '"]').attr('checked', 'checked');
    });
});

This is working but only on first click, i want it to loop whenever i clicked on 'Independent' button .ind , 'Love Partylist' button .lp and 'UNO' .uno it will select all the members of the selected party list (one click vote). Im wondering why it is only working only on first click, any help will be much appreciated. Thanks in advance!

radio buttons, or option buttons as they are referred to these days, are designed so that only one is selected at any time. Clicking one should deselect all others.

If you are allowing multiple entries you should really be using checkboxes instead.

When you programatically set checked on a radio button you have to programatically remove it.

in each of your event handlers remove the checked attribute from the lists that you don't want selected as well as setting the attribute on the list you do want selected.

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