【发布时间】:2015-10-24 01:33:19
【问题描述】:
我正在做一个可以使用 Jquery .clone() 克隆的问卷。我正在使用复选框来回答使用是或否的问题。我的问卷在表格内,然后使用以下代码:
$(':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>
number 显示你的重复次数。我知道单选按钮最适合这个,但我需要使用复选框。我认为脚本应该不会比我做的更复杂。
【问题讨论】:
-
这还不够完整,无法回答 - 你能创建一个jsfiddle,克隆的有错误吗?当您尝试从同一行中选择 2 时会发生什么?您可以选择所有选项还是不选择,或者这些选项是否也会影响第一份问卷?我认为将需要按钮周围的代码,
-
我正在使用 Laravel,所以在那里创建很复杂,但主要问题是如何避免重复单选按钮的值。因为在克隆函数中,名称/id 与 _2、_3 或其他值连接。但是我必须找到清除单选按钮值的方法,但仅限于生成的新组。
标签: jquery html laravel checkbox checkboxlist