如何使用 jquery 从动态表单向服务器发送请求

How do I send request to server from a dynamic form using jquery

我正在开发涉及产品销售的库存管理系统

表格是动态的

当用户点击添加新产品时,它会发送到服务器并获取产品价格

我的HTML代码

<form role="form" action="{{ url('admin/sales/store') }}" method="get">
    @csrf
    <div class="card-body">
        <div class="row">
            <div class="col-md-12">
            </div>
            <div class="col-md-12 right my-3">
                <a href="#" class="btn btn-danger" id="add-product">Add New Product</a>
            </div>
            <div id="wrapper" class="col-md-12">
                <div id="new-field" class="col-md-12">
                    <div class="row">

                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Product Name</label>
                                <select name="product[]" id="product" class="form-control">
                                    <option value="">Select Product Name</option>
                                    @foreach ($products as $product)
                                        <option value="{{ $product->id }}" name="product[]">
                                            {{ $product->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Quantity</label>
                                <input type="text" name="quantity[]" class="form-control"
                                    value="{{ old('quantity') }}"
                                    placeholder="Enter Quantity">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Unit Price</label>
                                <input type="text" name="price[]" disabled
                                    id="price"
                                    class="form-control"
                                    placeholder="Enter Quantity">
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="card-footer">
        <button type="submit" class="btn btn-primary float-md-right">Add Sales</button>
    </div>
</form>

JQUERY

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        $("#add-product").click(function(e) {
            e.preventDefault();
            $("#new-field").clone().appendTo("#wrapper");
        });
        $("#product").change(function(e) {
            e.preventDefault();
            axios.get("/ims/api/get-price/" + $(this).val())
                .then(function(response) {
                    $("#price").val(response.data.price);
                });
        })
</script>

JSON

{
   price: 34000.42
}

但只有第一行有效

如何使其他行起作用?

问题是您正在将 id 属性用于 select 您的元素。

最初,您的产品行(简化)如下所示:

<div id="wrapper">
    <div id="new-field">
        ...
        <select name="product[]" id="product"></select>       
        ...
        <input type="text" name="quantity[]" value="{{ old('quantity') }}" >
        ...
        <input type="text" name="price[]" disabled id="price">
        ...
    </div>
</div>

单击 #add-product 元素后,克隆整个 #new-field 元素,并使用以下代码将其附加到 #wrapper 元素:

$("#add-product").click(function(e) {
    e.preventDefault();
    $("#new-field").clone().appendTo("#wrapper");
});

那么您的产品行的简化视图如下所示:

<div id="wrapper">
    <div id="new-field">
        ...
        <select name="product[]" id="product"></select>       
        ...
        <input type="text" name="quantity[]" value="{{ old('quantity') }}" >
        ...
        <input type="text" name="price[]" disabled id="price">
        ...
    </div>
    <div id="new-field">
        ...
        <select name="product[]" id="product"></select>       
        ...
        <input type="text" name="quantity[]" value="{{ old('quantity') }}" >
        ...
        <input type="text" name="price[]" disabled id="price">
        ...
    </div>
</div>

请特别注意,new-fieldproductprice.

具有 doubled-up id 属性

id 应该在 HTML.

中是唯一的

然后,当您的价格更新代码运行时,它总是会导致问题。您浏览器中的 HTML 引擎忽略了所有重复的 id 属性:

// This line tries to get the unique element with the id "product"
// after it's found it (i.e. the first element in the DOM), it stops
// so this line only selects the first element with an id of "product"
$("#product").change(function(e) {
    e.preventDefault();
    axios.get("/ims/api/get-price/" + $(this).val())
        .then(function(response) {

            // This line tries to get the unique element with the id
            // "price", after it finds the first one, it stops looking
            // so this will never select the duplicated elements.
            $("#price").val(response.data.price);

        });
})

要修复它,您需要停止使用 id 到 select 产品。做这样的事情:

#new-field#product#price 元素上的 id 属性更改为 class:

<div class="new-field">
    <select name="product[]" class="product"></select>
    ...
    <input type="text" name="price[]" disabled class="price">
</div>

并根据class:

修改你的JS为select
$(".wrapper").on('change', '.product', function(e) {
    e.preventDefault();
    axios.get("/ims/api/get-price/" + $(this).val())
        .then(function(response) {

            $(this).parent('.new-field').find('.price').val(response.data.price);

        });
})

您还需要更新 JavaScript 才能正确添加新产品。