码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-10 14:24:46      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:type   cli   action   text   html   else   纽约   this   div   

<!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元素内容改变

标签:type   cli   action   text   html   else   纽约   this   div   

原文地址:http://www.cnblogs.com/sunxirui00/p/7500844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!