[英]How to submit form with dynamic html Input texbox using jQuery Ajax
我正在使用jQuery创建动态多个HTML输入框并填充值,填充后我想在每个文本框中更新产品价格,并使用ajax将更新后的值提交给服务器。 我面临着以下代码的问题,如果我在文本框中编辑产品价格并点击更新按钮,它调用我的ajax功能,但更新产品价格不发送到服务器。 我变得空虚了。
如果我单击“更新”按钮,我将获得以下代码的空数组:
JSON.stringify($("#updateNameForm").serializeArray())
出于某种原因,我的动态文本框更新了不会出现在ajax方法中的值。
请找到我的以下代码 - 有人可以帮助我在这里做错了以及如何解决问题。 完整代码:
来自服务器端的JSON:
[{
"productName": "Product1",
"productPrice": "323"
}, {
"productName": "Product2",
"productPrice": "4333"
}]
HTML代码:
<div class="content-wrapper">
<section class="content">
<div class="row">
<div class="col-md-9">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">My Form</h3>
</div>
<!-- /.box-header -->
<form id="updateNameForm" class="form-horizontal" method="post">
<div class="box-body">
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" id="editName" class="input_control" /> <strong> Edit</strong>
</label>
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-default">Cancel</button>
<input id="updateName" type="button" name="updatea" value="Update" class="btn btn-info pull-right">
</div>
</form>
</div>
</div>
</div>
</section>
</div>
更新Ajax
$("#updateName").on('click', function() {
$.ajax({
url: 'myApp/updateProduct',
type: "PUT",
dataType: 'json',
data: JSON.stringify($("#updateNameForm").serializeArray()),
success: function(result) {
alert(result);
},
error: function(e) {
console.log(e.responseText);
}
});
});
loadProduct函数
function loadProduct() {
$.ajax({
url: 'myApp/getProduct',
type: "GET",
dataType: 'json',
success: function(productJson) {
$.each(JSON.parse(JSON.stringify(productJson)), function(idx, obj) {
var formgroup = $("<div/>", {
class: "form-group"
});
formgroup.append($("<label>", {
class: "col-sm-2 control-label",
text: obj.productName
}));
var colsm = $("<div/>", {
class: "col-sm-10"
});
var input = $("<input/>", {
type: "text",
class: "form-control",
id: obj.productName + "Id",
value: obj.productPrice
});
colsm.append(input);
formgroup.append(colsm);
$(".box-body").append(formgroup);
});
},
error: function(e) {
console.log(e.responseText);
}
});
}
谢谢!
您没有为输入字段指定name
。
var input = $("<input/>", {
type: "text",
name: "productPrice",
class: "form-control",
id: obj.productName + "Id",
value: obj.productPrice
});
试试上面的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.