<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>checkbox</title>
    <script src="jquery.js"></script>
</head>
<body>
<p>我想去<span >_____</span></p>
<form action="#" >
    <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>

 

相关文章:

  • 2021-12-18
  • 2022-02-23
  • 2022-12-23
  • 2021-07-08
  • 2021-08-18
  • 2021-11-30
  • 2022-12-23
猜你喜欢
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-01-12
  • 2022-01-16
相关资源
相似解决方案