<form name="input" action="/test/compare method="get">
<input type="checkbox" name="id" value="val1">val1<br/>
<input type="checkbox" name="id" value="val2">val2<br/>
<input type="submit" value="Submit">
</form>
我想要一个如下所示的查询字符串:
/test/compare?id=val1,val2
这可能吗?
答案 0 :(得分:1)
不完全是逗号分隔值,但您可以获得所有选中复选框的值数组,如下所示。
<form name="input" action="/test/compare" method="get">
<input type="checkbox" name="id[]" value="val1">val1<br/>
<input type="checkbox" name="id[]" value="val2">val2<br/>
<input type="submit" value="Submit">
</form>
<强> PHP 强>
// here you'l get array of id in $_GET and process according to your need
$ids=$_GET['id'];
$str=implode(",",$ids); //array to comma separated values
echo $str; //output will be checked checkbox values--> val1,val2
修改强>
使用JAVASCRIPT的另一种解决方案
<form name="input" action="/test/compare" method="get">
<input type="checkbox" name="id" value="val1">val1<br/>
<input type="checkbox" name="id" value="val2">val2<br/>
<input type="submit" value="Submit">
</form>
<!-- load jQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
$("form[name=input]").submit(function(){ //bind onSubmit handler on page ready
//get all field values
var query="id="; //write here parameter name
$("form[name=input] [name=id]:checked").each(function(i){ //it will check for all elements inside form with name "id"
query+=$(this).val()+",";
});
query=query.slice(0,-1); //remove last extra comma
//redirect url by passing "query" as GET parameter
window.location=$(this).attr("action")+"?"+query;
return false;
});
});
</script>
答案 1 :(得分:0)
你可以让name="id[]"
使表单参数成为一个数组,但除此之外我不相信它可以做你要求的。除非它们是数组,否则您应该将唯一值作为表单字段。