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

I am working on an inventory management system that involves sales of products

The form is dynamic

when a user click on add new product it sends to the server and get the price of the product

My HTML Code

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

enter image description here

but it is only the first row that works

How can I make other rows to work?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source