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

checkbox选择post之后进行处理

时间:2017-09-07 11:02:07      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:check   php   js   

前端代码:

<form method="post" action="" onsubmit="set()">
            <div class="layui-form-item">

 <input  type="checkbox" value="1" name="parents" title="1">
 <input  type="checkbox" value="2" name="parents" title="2">
 <input  type="checkbox" value="3" name="parents" title="3">

                <input type="hidden" name="parent_cat" id="parent" value="">

            </div>

            <div class="layui-form-item">
                <button class="layui-btn" style="width:300px"  type="submit">下一步</button>

</span>
            </div>

        </form>
        
        
<script>

    function set() {
        var parents = check_arr(‘parents‘);
        $(‘#parent‘).val(parents);
    }

    function check_arr($str) {
        var str=document.getElementsByName($str);
        var objarray=str.length;
        var chestr="";
        for (i=0;i<objarray;i++)
        {
            if(str[i].checked == true)
            {
                chestr+=str[i].value+",";
            }
        }
        return chestr;
    }

</script>

后台代码:

$parent_cat = $_POST[‘parent_cat‘];
//删除最后一个逗号(post过来的是"1,2,3,")
//处理之后变为("1,2,3")
$parent_cat = substr($parent_cat,0,strlen($parent_cat)-1);
//然后可以分成数组进行处理
$pros = explode(‘,‘,$parent_cat);


checkbox选择post之后进行处理

标签:check   php   js   

原文地址:http://itafei.blog.51cto.com/10862892/1963321

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