首页 > 解决方案 > 如何使 GET 从 "field=foo&field=bar" 到 "field=foo,bar"

问题描述

我有一个包含许多字段的 html 表单:

<form action="" method="get">
...
<input type="checkbox" name="price" id="price-1000" value="1000">
<input type="checkbox" name="price" id="price-2000" value="2000">
<input type="checkbox" name="price" id="price-3000" value="3000">
...
<input type="submit" value="send">
</form>

在检查并发送时,我有以下查询字符串:?price=1000&price=2000

如何?price=1000,2000在浏览器 URL 中创建

我想我需要通过 htaccess 重写规则?还是有其他方法?

标签: php.htaccessurl-rewritingget

解决方案


使用 Jquery 将是可能的。

索引.php

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<form action="redex.php" method="get">
<input type="checkbox" id="price-1000" value="1000" class="ckbox">
<input type="checkbox" id="price-2000" value="2000" class="ckbox">
<input type="checkbox" id="price-3000" value="3000" class="ckbox">
<input type="hidden" name="price" id="price-1000" value="1000" class="ckvalues">
<input type="submit" value="send">
</form>
<script type="text/javascript">
    $(document).ready(function() {
        var favorite = [];
        $('.ckbox').change(function() {
            $.each($("input[type='checkbox']:checked"), function(){            
                favorite.push($(this).val());
            });
            var str1 = favorite.toString()
            $('.ckvalues').val(str1)
            favorite.length = 0;
        });

    });
</script>

redex.php

<?php
echo $_GET['price'];
?>

愿这对你有所帮助。


推荐阅读