我有一个输入就像
的表单<input type="text" value="" name="fieldval[3][4][]" >
现在,当我使用jquery序列化此表单时,我得到的结果是这样的
app_id=1&app_id=2&app_id=3&fieldval%5B2%5D%5B2%5D%5B%5D=1&fieldval%5B2%5D%5B2%5D%5B%5D=day&fieldval%5B3%5D%5B4%5D%5B%5D=23&fieldval%5B1%5D%5B1%5D%5B%5D=1&fieldval%5B1%5D%5B1%5D%5B%5D=mb&fieldval%5B1%5D%5B3%5D%5B%5D=200&fieldval%5B1%5D%5B3%5D%5B%5D=mb
请告诉我如何从这种类型的输入字段以正确的方式获取值。
现在当我使用serializeArray时,我得到像
这样的数组 Array
(
[step_number] => 4
[app_id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[formdata] => Array
(
[0] => Array
(
[name] => app_id[]
[value] => 1
)
[1] => Array
(
[name] => app_id[]
[value] => 2
)
[2] => Array
(
[name] => app_id[]
[value] => 3
)
[3] => Array
(
[name] => fieldval[2][2][]
[value] => 1
)
[4] => Array
(
[name] => fieldval[2][2][]
[value] => day
)
[5] => Array
(
[name] => fieldval[3][4][]
[value] => 23
)
[6] => Array
(
[name] => fieldval[1][1][]
[value] => 1
)
[7] => Array
(
[name] => fieldval[1][1][]
[value] => mb
)
[8] => Array
(
[name] => fieldval[1][3][]
[value] => 200
)
[9] => Array
(
[name] => fieldval[1][3][]
[value] => mb
)
[10] => Array
(
[name] => plan_type
[value] => free
)
[11] => Array
(
[name] => price
[value] =>
)
[12] => Array
(
[name] => sell_type
[value] => up
)
)
)
现在我想将此数组格式更改为喜欢这个
Array
( [app_id] =&gt;排列 ( [0] =&gt; 1 [1] =&gt; 2 [2] =&gt; 3 )
[fieldval] => Array
(
[2] => Array
(
[2] => Array
(
[0] => 1
[1] => day
)
)
[3] => Array
(
[4] => Array
(
[0] => 23
)
)
[1] => Array
(
[1] => Array
(
[0] => 1
[1] => mb
)
[3] => Array
(
[0] => 200
[1] => mb
)
)
)
[plan_type] => free
[price] =>
[sell_type] => up
)
我该怎么做
答案 0 :(得分:0)
使用ID为此的表单标记。
<form id="my-form-name" method="POST" action="http://something.com/">
<!-- Your fields here-->
<input type="text" value="" name="fieldval[]" />
<input type="text" value="" name="fieldval[]" />
</form>
在你的JS中
var data =$("#my-form-name").serializeArray();
console.log(data);
现在你有一个包含输入数据的数组。
答案 1 :(得分:0)
这是完整的例子
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<form id="frm">
<input type="text" value="3" name="fieldval[1][4][]" >
<input type="text" value="43" name="fieldval[2][4][]" >
<input type="text" value="22" name="fieldval[3][4][]" >
<input type="text" value="65" name="fieldval[4][4][]" >
<input type="button" value="click" id="click">
</form>
<div id="results"></div>
<script>
$(document).ready(function(){
$("#click").click(function(){
var data =$("#frm").serializeArray();
var fields = $( ":input" ).serializeArray();
jQuery.each( fields, function( i, field ) {
$( "#results" ).append( field.value + " " );
});
});
});
</script>
我希望这会有所帮助