试试看,
PHP
<?php
$querystring=explode('&',$_SERVER['QUERY_STRING']);
$newData=array();
foreach($querystring as $keyPair)
{
$kparr=explode('=',$keyPair);
$key=trim($kparr[0]);
$value=trim($kparr[1]);
if($key and $value)
{
$newData[$key]=$value;
}
}
print_r($newData);// your final array having valid key values
?>
HTML
<form action="#" method="GET" onsubmit="return false">
<div>
<input type="text" name="key[]" value="key1" /> <input type="text" name="value[]" value="value1" />
</div>
<div>
<input type="text" name="key[]" value="key2" /> <input type="text" name="value[]" value="value2" />
</div>
<div>
<input type="text" name="key[]" value="key3" /> <input type="text" name="value[]" value="value2" />
</div>
<div>
<input type="submit" id="btnSubmit" value="Save" />
</div>
</form>
脚本
$(function(){
$('#btnSubmit').on('click',function(){
var qs=[];
$('input[name="key[]"]').each(function(index){
k=$(this).val();
v=$('input[name="value[]"]:eq('+index+')').val();
if(k && v){// only for non-empty key value pair
qs.push(k+'='+v);
}
});
alert(qs.join('&'));
return false;
});
});
小提琴 http://jsfiddle.net/PdkSd/1