在复选框的索引中返回 1
going back 1 in index of checkbox
http://jsfiddle.net/thetylercox/LgxPn/27/
如果我单击 1 然后 2,则复选框未选中,并且会显示警报,如果我再次单击 2 没有警报!我确定我存储了 2 上一次检查的值,我需要撤消此操作!
<script>
$(function() {
var lastChecked = [];
$(':checkbox').change(function() {
if (this.checked) {
if (lastChecked.length && this.value != lastChecked[0].value) {
$(this).prop("checked", false)
alert("the last box you checked has a different value");
}
lastChecked.unshift(this);
}
else {
lastChecked.splice(lastChecked.indexOf(this), 1);
}
});
});
</script>
</head>
<body>
1
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)"
value="1" class="chk" id="chk<?php echo $a++?>" title="<?php echo
$rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="
<?php echo $rspatient['name']?>"/><br/>
2
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)"
value="2" class="chk" id="chk<?php echo $a++?>" title="<?php echo
$rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>"
lang="<?php echo $rspatient['name']?>"/><br/>
http://jsfiddle.net/thetylercox/LgxPn/27/
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function() {
var lastChecked = [];
$(':checkbox').change(function() {
if (this.checked) {
if (lastChecked.length && this.value != lastChecked[0].value) { $(this).prop("checked", false)
alert("the last box you checked has a different value");
}
lastChecked.unshift(this);
}
else {
lastChecked.splice(lastChecked.indexOf(this), 1);
}
});
});
</script>
</head>
<body>
1
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="1" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
2
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="2" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
3
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="3" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
1
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="1" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
2
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="2" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
3
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="3" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
1
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="1" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
2
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="2" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
3
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="3" class="chk" id="chk<?php echo $a++?>" title="<?php echo $rspatient['first'],' ',$rspatient['frameman'],' ', $rspatient['framemodel']?>" lang="<?php echo $rspatient['name']?>"/><br/>
</body>
</html>
由于您取消选中该框,因此不应将该复选框存储在lastChecked
数组中。 只需将lastChecked.unshift(this)
包裹在else
块中即可。
http://jsfiddle.net/LgxPn/28/
相关文章:
- 在从索引位置返回的字符串中查找空白
- 使用共享工具提示时,单击Highcharts柱形图会返回不正确的序列索引
- 如何比较两个数组,然后返回差异的索引
- 在 jquery 选项卡中查找 span 标记并返回该索引
- 如何获取从循环返回的元素的索引
- 需要呈现MongoDB查询返回结果的特定索引/位置
- 通过getElementById查找所选索引是't返回正确的值
- 按流星空格键模板中的索引返回数组项
- 辅助功能键盘专用选项卡到z索引“;返回顶部”;
- JavaScript:通过其他数组元素的索引访问ArrayElement?array[array[0]返回undefin
- 如果我对索引进行硬编码,它会按预期工作,为什么这个增量器会返回NAN
- 为什么不't此循环返回到第一个索引
- Javascript 返回索引的负数
- 如何检查数组中的重复值并用JavaScript返回索引
- 我如何从PHP返回索引二维数组到JavaScript
- 为什么这个jQuery返回索引3
- Angular ng-options返回索引而不是对象值
- 当字符匹配模式时从数组返回索引
- Javascript承诺递归返回索引
- jQuery.每个返回索引数组