简体   繁体   English

如何将输入复选框中的值传递给 jquery 中的 URL 参数

[英]How to pass the value from input checkbox to URL Parameters in jquery

I am trying to update the url Parameters based on the input selected of the form.我正在尝试根据表单中选择的输入更新 url 参数。

<form method="get" action="current.php" id="header-form">

<input type="hidden" name="location" value="location">
<input type="checkbox" name="type[]" class="type" value="opt1"> option1
<input type="checkbox" name="type[]" class="type" value="opt2"> option2

<input type="checkbox" name="type2[]" class="type1" value="new1"> new1
<input type="checkbox" name="type2[]" class="type1" value="new2"> new2

</form>

Jquery查询

By selecting the checkbox i want to append the values to the URL通过选择复选框,我想将值附加到 URL

My Jquery我的jQuery

<script type="text/javascript">
        $(document).ready(function() {
            $(".type").change(function() {
                $("#header-form").trigger("submit");
            });
             $(".type1").change(function() {
                $("#header-form").trigger("submit");
            });
        });
        
    </script>

If i select option1 it works fine and if i try to select the option 2, it removes the option 1 from the URL.如果我选择选项 1,它工作正常,如果我尝试选择选项 2,它会从 URL 中删除选项 1。

If i select the option 1 & option 2如果我选择选项 1 和选项 2

URL Should be -> http://localhost.current.php?type=option1&option2 URL 应该是 -> http://localhost.current.php?type=option1&option2

And if i select all the values option1 & option 2 and new 1 & new 2如果我选择所有值 option1 & option 2 和 new 1 & new 2

URL Should be -> http://localhost.current.php?type=option1&option2?type1=new1&new2 URL 应该是 -> http://localhost.current.php?type=option1&option2?type1=new1&new2

And if i select the values option1 & new 1 & new 2如果我选择值 option1 & new 1 & new 2

URL Should be -> http://localhost.current.php?type=option1?type1=new1&new2 URL 应该是 -> http://localhost.current.php?type=option1?type1=new1&new2

You must keep the previous submit data.您必须保留之前提交的数据。
do this with PHP:用 PHP 做到这一点:

<form method="get" action="current.php" id="header-form">

    <input type="hidden" name="location" value="location">
    <input type="checkbox" name="type[]" <?php if(isset($_GET['type'][0])): ?>checked<?php endif; ?> class="type" value="opt1"> option1
    <input type="checkbox" name="type[]" <?php if(isset($_GET['type'][1])): ?>checked<?php endif; ?> class="type" value="opt2"> option2

    <input type="checkbox" name="type2[]" <?php if(isset($_GET['type2'][0])): ?>checked<?php endif; ?> class="type1" value="new1"> new1
    <input type="checkbox" name="type2[]" <?php if(isset($_GET['type2'][1])): ?>checked<?php endif; ?> class="type1" value="new2"> new2

</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM