我正在做一份问卷调查,可以使用Jquery .clone()克隆。我正在使用复选框来回答问题,使用yes或no。我的调查问卷在一个表中,然后使用以下代码:
$(':checkbox').on('change',function(){
var tr = $(this), name = tr.prop('name');
if(tr.is(':checked')){
$(this).closest('tr').find('input[type=checkbox]').not($(this)).prop('checked', false);
}我只能选中同一行(Tr)中的一个复选框,但问题出在我使用按钮克隆调查问卷时。脚本适用于第一个调查问卷,但不适用于第二个调查问卷。当被克隆时,生成一个新的id (previous_id_2)。
我尝试了很多方法,但我认为这个脚本不是最适合我的代码,因为它只适用于第一个。我需要一些像这样工作的东西:
If two checkboxes in same row -> only be able to select one.
>And something that should be works for different div id, because it´s generated
a new id for each table, the structure of the html is:
<div id="questionnaire {{ number }}">
<table class="table quest">
<thead></thead>
<tbody>
<tr>
<td><checkbox 1 id="q_Q1_y{{ number }}" name="q_Q1_y{{ number }}" class="table quest yes" ></td>
<td><checkbox 2 id="q_Q1_n{{ number }}" name="q_Q1_n{{ number }}" class="table quest"></td>
</tr>
<tr>
<td><checkbox 1 id="q_Q2_y{{ number }}" name="q_Q1_y{{ number }}" class="table quest yes"></td>
<td><checkbox 2 id="q_Q2_n{{ number }}" name="q_Q2_n{{ number }}" class="table quest"></td>
</tr>
</tbody>数字显示了重复的次数。我知道单选按钮最适合这样做,但我需要使用复选框。我认为脚本不应该比我更复杂。
发布于 2015-08-02 23:08:03
为什么要使用<checkbox>来表示是和否?如果两者都不能检查,你应该使用单选按钮-或者你的<checkbox>标签实际上是一个单选按钮?您还引用了这里没有的CSS。这里发布的代码应该是Minimal, Complete and Verifiable的-通过这个过程可以帮助你成为一名更好的程序员。您可能需要将问题调整为可以通过jsfiddle发布的表单,jsfiddle允许使用jquery库。
https://stackoverflow.com/questions/31761959
复制相似问题