简体   繁体   中英

I have created a dynamic checkbox using php, but when i use .checked() to check it, only first radio button is checked. how to check the rest?

<div  id="pop2" class="box2" style="display: none">
    <form action="" method="post">
        <div class="second" style="margin-top:3px;margin-left:10px;font-size:100%">Add to</div>
<?php 
    $new2=array();
    $new2=getlist();
    foreach($new2 as $value){ 
        echo'<input type="radio" id="qwerty" name="qwerty" value="'.$value.'" onclick="theLast()"  />';

        echo"$value";echo"<br>";}
?>
        <hr>
        <button type="button" id="add2" class="new" onclick="theFinal()" >Create new list</button>
  </form>
</div>

and i used javascript to check in theLast() function

  if (document.getElementById('qwerty').checked) {
      window.lists=document.getElementById('qwerty').value;
  }

only first radio button is working fine, rest are not detected in .checked()

Having the same id attribute for all the checkboxes can cause these kinds of issues. I would suggest using a commom class instead and iterating the checkboxes using the getElementByClassName function to check for "checked" boxes.

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