Javascript 在复选框的索引中返回1

Javascript 在复选框的索引中返回1,javascript,jquery,Javascript,Jquery,如果我单击1然后单击2,复选框将被取消选中,并显示一条警报。如果我再次单击2,则问题是无警报!我确定我在2上存储了上一次检查的值,我需要撤销它 <script> $(function() { var lastChecked = []; $(':checkbox').change(function() { if (this.checked) { if

如果我单击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/>

由于取消选中该复选框,因此不应将该复选框存储在lastChecked数组中。只需将lastChecked.unshiftthis包装在else块中


我尝试在警报显示$this.value=lastChecked[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>​