简体   繁体   English

如何使用aquery仅调用特定更改的ajax字段?

[英]How to call ajax only the specific field made changes using jquery?

Here is the ajax call function for update the data.In this all delivered_qty fields are updating when calling updateData() function.Instead of that i need to update only the specific field(delivered_qty) if it is not match the criteria.Now within this condition if (!match) {} all fields are updating. 这是用于更新数据的ajax调用函数。在这种情况下,调用updateData()函数时所有的delivery_qty字段都将更新。如果不符合条件,我只需要更新特定的字段(delivered_qty)。条件if(!match){}所有字段都在更新。 how to update only the particular field if it is not match instead of updating all the fields? 如果不匹配,如何只更新特定字段而不是更新所有字段?

function updateData() { //update data
    var delivered_qty = [];
    var delivered_status = [];
    var deli_qtydbvalue = [];

    $('.delivered_status').each(function() {
        delivered_status.push($(this).val());
    });
    $('.delivered_qty').each(function() {
        delivered_qty.push($(this).val());
        deli_qtydbvalue.push($(this).attr('orginaldbvalue'));
    });

    var match = (delivered_qty.length == deli_qtydbvalue.length) && delivered_qty.every(function(element, index) {
        return element === deli_qtydbvalue[index];
    });

    if (!match) { 
        $.get("updatedeliverystatus.php", {
            delivered_qty: delivered_qty,
            invoiceitemsID: invoiceitemsID,
            delivered_status: delivered_status,
            getinvoiceno: getinvoiceno
        }).done(function(data) {
            if (data == "1") {
               //some code
            }

        }); 
    } //end if not match

    //if(match){
    //  alert("matching")
    //}

}

Since I don't have a proper code of your application, I just create a simple implementation. 由于我的应用程序代码不正确,因此我只创建了一个简单的实现。 Here you don't need that !match condition, instead I have use two new variables to keep updated rows only and passing it to the server. 在这里,您不需要!match条件,而是使用了两个新变量来仅保留更新的行并将其传递到服务器。 Hope this implementation will give you a solution. 希望此实现将为您提供解决方案。

 function updateData() { //update data var delivered_qty = []; var delivered_status = []; var deli_qtydbvalue = []; // new variables to keep updated data var updatedDelivered_qty = []; var updatedDelivered_status = []; $('.delivered_status').each(function() { delivered_status.push($(this).val()); }); $('.delivered_qty').each(function() { delivered_qty.push($(this).val()); deli_qtydbvalue.push($(this).attr('data-orginaldbvalue')); }); // Only filter out updated rows if(delivered_qty.length == deli_qtydbvalue.length){ for(var i = 0; i<delivered_qty.length; i++ ) if(delivered_qty[i] != deli_qtydbvalue[i]){ updatedDelivered_qty.push(delivered_qty[i] ); updatedDelivered_status.push(delivered_status[i]); } } alert(updatedDelivered_qty); alert(updatedDelivered_status); // You don't need if condition here, but use that function that is using to call server } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="itemsdetails" class="itemsdetails" style="clear: both; width: 100%; margin: 1px 0 0 0; border: 0px solid black;"> <thead> <tr> <th>Id</th> <th>Quantity</th> <th>Status</th> </tr> </thead> <tbody> <tr> <td>1</td> <td><input data-orginaldbvalue="10" type="text" class="delivered_qty" value="10"/></td> <td><input type="text" class="delivered_status" value="Complete"/></td> </tr> <tr> <td>2</td> <td><input data-orginaldbvalue="20" type="text" class="delivered_qty" value="20"/></td> <td><input type="text" class="delivered_status" value="Complete"/></td> </tr> <tr> <td>3</td> <td><input data-orginaldbvalue="30" type="text" class="delivered_qty" value="30"/></td> <td><input type="text" class="delivered_status" value="Complete"/></td> </tr> </tbody> </table> <button onclick="updateData()">Update</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM