我有一组复选框,它们代表代码片段中的行。我很想通过两次单击(在代码的第一行和最后一行)复选框来选择该代码的一部分,检查中间的代码并设置两个输入字段以及选择的开始行和结束行。这是我的代码,但它似乎根本不起作用。
代码
<div id="example">
<input type="checkbox" name="box1" id="box1">
<label for="box1"> code line 1 </label>
<input type="checkbox" name="box2" id="box2">
<label for="box2"> code line 2 </label>
<input type="checkbox" name="box3" id="box3">
<label for="box3"> code line 3 </label>
<input type="checkbox" name="box4" id="box4">
<label for="box4"> code line 4 </label>
<input type="checkbox" name="box5" id="box5">
<label for="box5"> code line 5 </label>
</div>
<form name="addhint" method="post" action="">
<input type="hidden" name="start" value="0">
<input type="hidden" name="stop" value="0">
[...]
</form>
查询代码
$('#example input:checkbox').click(function() {
var $this = $(this);
var $id= $this.id;
if ($this.is(':checked')) {
if ($('#form input[name=start]').val() == 0) {
$('#form input[name=start]').val($id);
}
else {
var $start= $('input[name=start]').val();
if ($id - $start > 1) {
for ($i = $start+1; $i < $id; $i++) {
$('#example input[id=box'+$i+']').attr('checked', true);
$('#example input[id=box'+$i+']').attr('disabled', 'disabled');
}
$('#form input[name=stop]').val($id);
}
}
} else {
// do other stuff
}
});
当我单击一个复选框时,什么也没有发生。这是我第一次使用 jQuery,很多事情都可能出错。只是不要杀我.. :)