当前位置: 移动技术网 > IT编程>开发语言>JavaScript > checkbox:click事件触发span元素内容改变的方法

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

示例如下:

<!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>

以上这篇checkbox:click事件触发span元素内容改变的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网