如何选择匹配行的输入标记内的文本的行

时间:2015-07-30 11:32:00

标签: jquery

我有一个动态添加行的表,其中td中有文本框。我想要做的是在添加第二行之前它应该检查以前的文本框中是否已经存在,如果是,则在行中的另一个文本框中将值添加到当前值,否则添加新行

我试过的是

if (tdlength != 0) {
        $('tr.invoicerow').each(function () {
            var RowProductNames = $(this).find("input.invoiceproduct").val();
            if (RowProductNames == InvoiceProductName) {
                // Sum The Rate to Value alredy exists In text box
            } else {
                row = "<tr class='invoicerow'><td hidden><input class='dcid' value=" + deliverychallanid + "></td><td><input class='case invoicechkbox' type='checkbox'></td><td><input class='form-control invoiceproduct' id='invoiceproduct_" + rowno + "' value=" + InvoiceProductName + "></td><td><input class='form-control invoiceunit' id='invoiceunit_" + rowno + "' value=" + InvoiceUnit + "></td><td><input class='form-control invoicerate' id='invoicerate_" + rowno + "' value=" + InvoiceRatePerUnit + "></td><td><input class='form-control invoicenos' id='invoicenos_" + rowno + "'value=" + 0 + "></td><td><input class='form-control invoicequantity' id='invoicequantity_" + rowno + "' value=" + 0 + "></td><td><input class='form-control invoicecapacity' id='invoicecapacity_" + rowno + "'value=" + value.Capacity + "></td><td><input class='form-control invoiceamount' id='invoiceamount_" + rowno + "' value=" + Totalamount + "></td></tr>";
                $('#invoicebody').append(row);
            }
        });
        } else {
            row = "<tr class='invoicerow'><td hidden><input class='dcid' value=" + deliverychallanid + "></td><td><input class='case invoicechkbox' type='checkbox'></td><td><input class='form-control invoiceproduct' id='invoiceproduct_" + rowno + "' value=" + InvoiceProductName + "></td><td><input class='form-control invoiceunit' id='invoiceunit_" + rowno + "' value=" + InvoiceUnit + "></td><td><input class='form-control invoicerate' id='invoicerate_" + rowno + "' value=" + InvoiceRatePerUnit + "></td><td><input class='form-control invoicenos' id='invoicenos_" + rowno + "'value=" + 0 + "></td><td><input class='form-control invoicequantity' id='invoicequantity_" + rowno + "' value=" + 0 + "></td><td><input class='form-control invoicecapacity' id='invoicecapacity_" + rowno + "'value=" + value.Capacity + "></td><td><input class='form-control invoiceamount' id='invoiceamount_" + rowno + "' value=" + Totalamount + "></td></tr>";
            $('#invoicebody').append(row);

    }

当我尝试这段代码时会发生什么 首先,它检查第一行是否文本匹配更改在另一个文本框中完成 再次检查第二行文本不匹配它添加新行并继续添加直到循环完成..如何解决此问题

由于

2 个答案:

答案 0 :(得分:1)

见到这一点。我认为这就是你想要的:

    var test = false;
    $('tr.invoicerow').each(function () {           
        var RowProductNames = $(this).find("input.invoiceproduct").val();
        if (RowProductNames == InvoiceProductName) {

            // Sum The Rate to Value alredy exists In text box
            test = true;
            return false;
        } 
    });
    if(!test){
        row = "<tr class='invoicerow'><td hidden><input class='dcid' value=" + deliverychallanid + "></td><td><input class='case invoicechkbox' type='checkbox'></td><td><input class='form-control invoiceproduct' id='invoiceproduct_" + rowno + "' value=" + InvoiceProductName + "></td><td><input class='form-control invoiceunit' id='invoiceunit_" + rowno + "' value=" + InvoiceUnit + "></td><td><input class='form-control invoicerate' id='invoicerate_" + rowno + "' value=" + InvoiceRatePerUnit + "></td><td><input class='form-control invoicenos' id='invoicenos_" + rowno + "'value=" + 0 + "></td><td><input class='form-control invoicequantity' id='invoicequantity_" + rowno + "' value=" + 0 + "></td><td><input class='form-control invoicecapacity' id='invoicecapacity_" + rowno + "'value=" + value.Capacity + "></td><td><input class='form-control invoiceamount' id='invoiceamount_" + rowno + "' value=" + Totalamount + "></td></tr>"
        $('#invoicebody').append(row);
    }

答案 1 :(得分:1)

您的代码应该是这样的,

invoicerate

很少的事情:我假设要设置的文本框类是tmp:。还假设用户在表单的其他部分输入数据。 如果没有,请澄清。