0

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/

4

2 回答 2

0

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

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

于 2012-06-28T00:40:13.180 回答
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>​
于 2012-07-15T06:59:25.823 回答