在复选框的索引中返回1

时间:2012-06-28 00:22:24

标签: javascript jquery

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/

2 个答案:

答案 0 :(得分:0)

由于您取消选中此框,因此不应将该复选框存储在lastChecked数组中。只需将lastChecked.unshift(this)包裹在else块中。

http://jsfiddle.net/LgxPn/28/

答案 1 :(得分:0)

<!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>​