I'm creating dynamic multiple HTML input box using jQuery and populating the value, after populating I want to update the product price in each text box and submit the form with updated value to server using ajax. I'm facing issue with below code, if I edit the product price in textbox and click on Update button its calling my ajax function, but updated product price not sending to server. I'm getting empty value.
我正在使用jQuery创建动态多个HTML输入框并填充值,填充后我想在每个文本框中更新产品价格,并使用ajax将带有更新值的表单提交给服务器。我面临下面的代码问题,如果我在文本框中编辑产品价格并点击更新按钮,它调用我的ajax功能,但更新产品价格不发送到服务器。我变得空虚了。
If I click Update button, I'm getting empty array for below code:
如果我单击“更新”按钮,我将获得以下代码的空数组:
JSON.stringify($("#updateNameForm").serializeArray())
For some reason my dynamic textboxes updated values not coming to ajax method.
出于某种原因,我的动态文本框更新了不会出现在ajax方法中的值。
Please find my below code - can someone help what I'm doing wrong here and how to resolve the issue. Complete code:
请找到我的下面代码 - 有人可以帮助我在这里做错了以及如何解决问题。完整代码:
JSON which comes from server side:
来自服务器端的JSON:
[{
"productName": "Product1",
"productPrice": "323"
}, {
"productName": "Product2",
"productPrice": "4333"
}]
HTML Code:
<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>
Update 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
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);
}
});
}
Thanks!
1 个解决方案
#1
0
You have not given name
to your input field.
您没有为输入字段指定名称。
var input = $("<input/>", {
type: "text",
name: "productPrice",
class: "form-control",
id: obj.productName + "Id",
value: obj.productPrice
});
Try with above code.
试试上面的代码。
#1
0
You have not given name
to your input field.
您没有为输入字段指定名称。
var input = $("<input/>", {
type: "text",
name: "productPrice",
class: "form-control",
id: obj.productName + "Id",
value: obj.productPrice
});
Try with above code.
试试上面的代码。