在复选框的索引中返回 1

going back 1 in index of checkbox

本文关键字:返回 索引 复选框      更新时间:2023-09-26

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/