JQuery 获取选中多选框的value,合并成字符串传给后台

简介: JQuery 获取选中多选框的value,合并成字符串传给后台
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>JQuery 获取选中多选框的value,合并成字符串传给后台</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
        <style>
        </style>
    </head>
    <body>
        <div class="leftType">
            <p>
                <input name="person" type="checkbox" value="1" checked='checked' />员工
            </p>
            <p>
                <input name="person" type="checkbox" value="2" checked='checked' />访客
            </p>
            <p>
                <input name="person" type="checkbox" value="3" checked='checked' />车辆
            </p>
            <p>
                <input name="person" type="checkbox" value="4" checked='checked' />承包商
            </p>
        </div>
        <button type="submit" id="submit">提交</button>
    </body>
    <script type="text/javascript">
        $("#submit").click(function() {
            //JQuery 获取选中多选框的value,合并成字符串传给后台
            //定义一个空数组存储
            var arr = [];
            //根据name的值获取到所有选中checkbox,并遍历
            $(".leftType input:checkbox[name='person']:checked").each(function(i) {
                //arr.push($(this).val());
                arr[i] = $(this).val();
            });
            console.log(arr);
            // 将数组合并成字符串
            arrType = arr.toString();
            alert(arrType);
            $.ajax({
                url: "/location/personsByType",
                data: {
                    //type : '1,2,3,4,',    
                    type: arrType,
                },
                type: "GET",
                success: function(data) {},
                error: function(err) {
                    console.log(err);
                }
            })
        })
    </script>
</html>
相关文章
|
10月前
|
JavaScript 前端开发
JQuery 获取选中多选框的value,合并成数组传给后台
JQuery 获取选中多选框的value,合并成数组传给后台
35 0
|
JavaScript
jquery提取字符串中的数字或非数字
jquery提取字符串中的数字或非数字
73 0
|
XML JavaScript 数据格式
JQuery 动态XML字符串添加节点
今天实现了动态的给一个XML字符串添加节点。
142 0
|
前端开发 JavaScript
ajax(jquery)前后台传数组(Springmvc后台)
ajax(jquery)前后台传数组(Springmvc后台)
164 0
ajax(jquery)前后台传数组(Springmvc后台)
|
JavaScript
jQuery $.merge()方法合并数组
jQuery $.merge()方法合并数组
129 0
jQuery $.merge()方法合并数组
|
JavaScript
jquery获取多个相同name的input的value值
jquery获取多个相同name的input的value值
|
JavaScript
后台扫描单号直接录入系统jquery实现方法
后台扫描单号直接录入系统jquery实现方法
112 0
后台扫描单号直接录入系统jquery实现方法
|
4月前
|
JavaScript
jQuery图片延迟加载插件jQuery.lazyload
jQuery图片延迟加载插件jQuery.lazyload
|
3月前
|
设计模式 JavaScript 前端开发
必知的技术知识:jQuery插件开发精品教程,让你的jQuery提升一个台阶
必知的技术知识:jQuery插件开发精品教程,让你的jQuery提升一个台阶
44 1
|
1月前
|
JavaScript 前端开发 数据安全/隐私保护
Validform jQuery插件详解
【8月更文挑战第21天】