我使用python在django做一个小项目。
在那里,我动态地填充一个包含待处理任务数据的表。表的每一行都包含一个复选框字段。根据优先级,行数可以更少或更多。 如果没有选中任何复选框,我必须停止提交按钮单击事件的回发操作。 如何在javascript代码中识别复选框名称或ID,因为它们是动态生成的。
请帮帮我..谢谢你的时间..
答案 0 :(得分:0)
<强> HTML 强>
<form method="post" class="myform" action=".">
<table>
<tbody>
<tr>
<th><label for="checkbox1">Checkbox 1</label></th>
<td><input type="checkbox" id="checkbox1" name="checkbox1" /></td>
</tr>
<tr>
<th><label for="checkbox2">Checkbox 2</label></th>
<td><input type="checkbox" id="checkbox2" name="checkbox2" /></td>
</tr>
<tr>
<th><label for="checkbox3">Checkbox 3</label></th>
<td><input type="checkbox" id="checkbox3" name="checkbox3" /></td>
</tr>
</tbody>
</table>
<input type="submit" class="myform-submit" />
</form>
javascript(假设是jquery)
$(document).ready(function(){
$(".myform-submit").bind("click", function(e) {
e.preventDefault();
var $form = $(this).closest(".myform");
if ($("input:checkbox:checked").length) {
alert("submitting");
$form.submit();
}
});
});
<强>的jsfiddle 强>