Javascript 如何获取动态创建的输入字段(Json)的值
根据用户输入,通过jquery创建输入字段 如果用户类型数量:5,则我创建了5个输入字段 例如,如果用户给定数量=3,那么这就是使用Jquery动态创建html的方式Javascript 如何获取动态创建的输入字段(Json)的值,javascript,jquery,json,Javascript,Jquery,Json,根据用户输入,通过jquery创建输入字段 如果用户类型数量:5,则我创建了5个输入字段 例如,如果用户给定数量=3,那么这就是使用Jquery动态创建html的方式 <tr id = "tr_1"> <td><input type="text" name="cont_no1" id="cont_no1" /><td> <td><input type="text" name="cont_size1" id="cont_si
<tr id = "tr_1">
<td><input type="text" name="cont_no1" id="cont_no1" /><td>
<td><input type="text" name="cont_size1" id="cont_size1" /><td>
<td><input type="text" name="cont_type1" id="cont_type1" /><td>
</tr>
<tr id = "tr_2">
<td><input type="text" name="cont_no2" id="cont_no1" /><td>
<td><input type="text" name="cont_size2" id="cont_size2" /><td>
<td><input type="text" name="cont_type2" id="cont_type2" /><td>
</tr>
<tr id = "tr_3">
<td><input type="text" name="cont_no3" id="cont_no3" /><td>
<td><input type="text" name="cont_size3" id="cont_size3" /><td>
<td><input type="text" name="cont_type3" id="cont_type3" /><td>
</tr>
现在我需要将所有这些输入字段值存储在json中
var jsonObj= jsonObj || [];
for(var i=1; i<cont_qty; i++)
{
item = {};
item ["cont_no"] = $('#cont_no'+i).val();
item ["cont_size"] = $('#cont_size'+i).val();
item ["cont_type"] = $('#cont_type'+i).val();
jsonObj.push(item);
}
var jsonObj=jsonObj | |[];
对于(变量i=1;ii;i++)
{
itemCount++;
//在表中动态创建行
自动变量
20
40
45
数码摄像
高清
HC
OT
FR
HT
射频
24000公斤
27000公斤
30480公斤
超重型
IICL
阿西斯
货物价值
';
$(“#munna”).append(auto#tr);
}
});
$(“#getButtonValue”)。单击(函数()
{
var jsonObj=jsonObj | |[];
对于(var i=1;i确保在通过jquery创建html后调用函数
createHtml(); // function to create the html
storeValuesToArray(); // Your function to store data to array
还要确保正确关闭标签
,并将
放入
标签内
并确保您的cont_quaty
设置为值在创建html并添加所有必要字段后,您可以使用以下选择器捕获所有元素:
var jsonObj=jsonObj | |[];
$('[name^=“cont_no”]')。每个(函数(){
var i=this.name.split('cont_no')[1];
var item={};
项目['cont_no']=$(this.val();
item['cont_size']=$('name=“cont_size'+i+'”).val();
item['cont_type']=$('name=“cont_type'+i+'”).val();
jsonObj.push(项目);
});
有小循环错误:)
for(var i=1;i)您的html无效。输入的最后一个id=name=“cont_size2”
是什么?抱歉编辑了…@phillip100I假设tr_2
中的id=“cont_no1”
也是一个打字错误(只是为了确保您有唯一的id)?您可以向我们展示用于创建这些输入字段的方法。
createHtml(); // function to create the html
storeValuesToArray(); // Your function to store data to array
for(var i=1; i<=cont_qty.value; i++)
{
alert(cont_qty.value);
item = {};
item ["cont_no"] = $('#cont_no'+i).val();
item ["cont_size"] = $('#cont_size'+i).val();
item ["cont_type"] = $('#cont_type'+i).val();
jsonObj.push(item);
}