search

Home  >  Q&A  >  body text

javascript - Dynamically generate radio, names conflict with each other

<button>添加ROW</button>
<table>
    <tr>
        <td>
            <input type="radio" name="HasFlower" class="yes">是
            <input type="radio" name="HasFlower" class="no">否
        </td>
    </tr>
</table>

<script>
$("button").click(function(){
    var chtml=$("table").children().clone(true);
    $(this).after(chtml);

    var nRow=$(this).siblings('tbody');
    for(var i=0;i<nRow.length;i++){
        nRow[i].setAttribute("data-myid",i+1);
        nRow.children("tr").find("input[class='yes']")[i].setAttribute("name","HasFlower"+i);
        nRow.children("tr").find("input[class='no']")[i].setAttribute("name","HasFlower"+i);
    };
});
</script>

Click to generate a new table and dynamically change the name for the radio. However, different names conflict with each other. For example, name=HasFlower1 and name=HasFlower2 cannot be selected at the same time. Why is this?

阿神阿神2699 days ago901

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 11:05:42

    reply
    0
  • Cancelreply