javascript - label标签绑定jQuery的click事件,但click事件会触发两次
PHP中文网
PHP中文网 2017-04-11 12:47:14
[JavaScript讨论组]

以下是我的代码,想实现点击单选项,获取得分的需求,但是点击单选项的文字,得分会被计算两次,请问我的代码出现了什么问题?谢谢

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
  <label class="man"><input type="radio" name="sex" id="man">man</label>
  <label class="woman"><input type="radio" name="sex" id="woman">woman</label>
  <script src="JS/jquery-3.1.1.min.js"></script>
  <script>
    $(document).ready(function(){
      var score = 0;
      $(".man").click(function(){
        score = score + 1;
        console.log(score);
        // score = 0;
      });

      $(".woman").click(function(){
        score = score + 2;
        console.log(score);
        // score = 0;
      });
    })
  </script>
</body>
</html>
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(2)
大家讲道理

我认为原因出在事件捕获
先说解决方案:在你的代码里,为click绑定的函数添加return false:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
  <label class="man"><input type="radio" name="sex" id="man">man</label>
  <label class="woman"><input type="radio" name="sex" id="woman">woman</label>
  <script src="JS/jquery-3.1.1.min.js"></script>
  <script>
    $(document).ready(function(){
      var score = 0;
      $("#man").click(function(){
        score = score + 1;
        console.log(score);
        // score = 0;
        /*修改部分开始*/
        return false;
        /*修改部分结束*/
      });

      $("#woman").click(function(){
        score = score + 2;
        console.log(score);
        // score = 0;
        /*修改部分开始*/
        return false;
         /*修改部分结束*/
      });
    })
  </script>
</body>
</html>

现在我们来验证一下猜测:假设是事件捕获,那么只需要在触发的时候弹出触发事件的对象即可。所以我们修改一下click绑定的函数:

$("#man").click(function(event){
    score = score + 1;
    console.log(score);
    // score = 0;
    /*修改部分开始*/
       console.log(event.target)
    /*修改部分结束*/
  });

看下结果:

//控制台打印的结果
1
<label class=​"man">​…​</label>​
2
<input type=​"radio" name=​"sex" id=​"man">​

问题已解决,就这么简单。

天蓬老师

为什么 不绑定到 input 上:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
  <label class="man"><input type="radio" name="sex" id="man">man</label>
  <label class="woman"><input type="radio" name="sex" id="woman">woman</label>
  <script src="JS/jquery-3.1.1.min.js"></script>
  <script>
    $(document).ready(function(){
      var score = 0;
      $("#man").click(function(){
        score = score + 1;
        console.log(score);
        // score = 0;
      });

      $("#woman").click(function(){
        score = score + 2;
        console.log(score);
        // score = 0;
      });
    })
  </script>
</body>
</html>

如果坚持要绑定到 label 上,因为他就是这样设定了label标签,神也救不了你了。

为了让你更快入坑,推你一把:

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号