如何使用jQuery Ajax提交带有动态HTML输入文本框的表单

4
我正在使用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>

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 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);
        }
    });
}

谢谢!


你是怎么发现 但更新后的产品价格没有发送到服务器 的问题的? - guradio
在提交表单时,我得到了空值。我放置了警告语句 - JSON.stringify($("#updateNameForm").serializeArray()) - 这返回空数组而不是更新后的值。 - java begineer
为什么不使用 $("#updateNameForm").serialize(),为什么? - madalinivascu
1个回答

0

您的输入字段未命名为name

var input = $("<input/>", {
    type: "text",
    name: "productPrice",
    class: "form-control",
    id: obj.productName + "Id",
    value: obj.productPrice
});

尝试使用上述代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接