当前位置:Gxlcms > JavaScript > click事件触发span元素内容改变的方法

click事件触发span元素内容改变的方法

时间:2021-07-01 10:21:17 帮助过:71人阅读

本文主要和大家分享checkbox:click事件触发span元素内容改变的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。

示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>checkbox</title>
 <script src="jquery.js"></script>
</head>
<body>
<p>我想去<span id="spanId">_____</span></p>
<form action="#" id="form">
 <input type="checkbox" name="city" value="南京">南京
 <input type="checkbox" name="city" value="北京">北京
 <input type="checkbox" name="city" value="纽约">纽约
</form>
<script>
  $(function () {
   $("input[name=city]").click(function () {
    var arr = [];
    $("input[name=city]").each(function () {
     if(this.checked){
      arr.push(this.value);
     }else{
      var index = arr.indexOf(this.value);
      if(index != -1){
       arr[index] = "";
      }
     }
    });
    $("#spanId").text(arr.join(","));
   })
  });
</script>
</body>
</html>

相关推荐:

Jquery对新插入的节点 绑定Click事件失效的解决方法

jquery事件mousedown和Click事件不能同时用?

JQuery不能使用click事件的问题解决

以上就是click事件触发span元素内容改变的方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行