jeudi 19 juillet 2018

Get input value from a table with array of inputs [ajax]

I have a html table which data are saved using ajax. The problem is that each row in the table accepts an input, I can get other rows value of the table but I can't get the inout value. Whatever i try it shows undefined or null value.

//table

   $(".add-row").click(function () {
                        var newid = id++;
                        $("#stocktable").append('<tr valign="top" id="' + newid + '">\n\
                        <td><input type=\'checkbox\' name=\'record\'></td>\n\
                        <td class="category' + newid + '">' + document.querySelector('#category option:checked').textContent + '</td>\n\
                        <td class="product' + newid + '">' + document.querySelector('#product option:checked').textContent + '</td>\n\
                        <td class="quantity' + newid + '">' + "<input type='text' id='qty' class='form-control' placeholder='Enter quantity here...'>" + '</td>\n\
                        <td class="product_id' + newid + '" style="display: none">' + $("#product").val() + '</td>\n\ </tr>');
                    });

The problem is with the quantity data which are received as input from user.

//this is how I am getting the other data

$("#buttonsave").click(function (e) {
                        e.preventDefault();

                        var lastRowId = $('#stocktable tr:last').attr("id"); //finds id of the last row inside table
                        var product_name = new Array();
                        var product_id = new Array();
                        var quantity = new Array();
                        for (var i = 1; i <= lastRowId; i++) {
                            product_id.push($("#" + i + " .product_id" + i).html());
                            product_name.push($("#" + i + " .product" + i).html()); //pushing all the names listed in the table
                            quantity.push($("#" + i + " .quantity" + i).html()); //pushing all the ages listed in the table
                        }
                        var sendproduct_id = JSON.stringify(product_id);
                        var sendproduct = JSON.stringify(product_name);
                        var sendquantity = JSON.stringify(quantity);

I tried to add this $("#qty").val() in the quantity.push but can not get the input.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire