0

当我将新的表格行插入表格时,我需要重命名一个复选框。为了皮特的爱,我不知道为什么这不起作用

$('.productSelect').blur(function() {
      $('#invoiceTable tbody>tr:last').clone(true)
          .insertAfter('#invoiceTable tbody>tr:last').find("input").val("")
          .find("select").val("").find("checkbox")
          .attr('name', "soldOut[" +rowCount + "]");
    rowCount++;
    return false;
    });

的HTML:

<tr>
<td><img src="/pics/deleteRow.png" class="delete"> </td>
<td class="productColumn">

    <select name="productID[]" class="productSelect" >
        <option value="0"></option>
    </select>

</td>


<td>
    <select name="lotID[]" class="lotNumber" >
        <option value=0>Lot #</option>
    </select>
</td>
<td>
    <select name="wheelID[]" class="wheelNumber">
        <option value=0>Wheel</option>
    </select>

</td>
<td>
    <select name="packageType[]" class="packageType">
        <option value=0>Pkg Type</option>
    </select>
</td>
<td class="numberPieces"><input name="numberPieces[]" class="numberOfPieces"></td>
<td class="quantityField"><input name="weight[]" class="weight" ></td>
<td class=priceField><input name="price[]" type="number" step="any">
</td>
<td class="subtotalField"><input type="number" step="any" readonly="readonly"></td>
<td class="soldOut"><input type="checkbox" name="soldOut[<?php echo $rowNum; ?>]" class="soldOutBox" ></td>

知道问题是什么吗?

4

1 回答 1

2

当您执行 find() 时,您需要执行.end()以返回堆栈,以便返回上一个元素

$('div').find('span') // you are now at the span level.. so you have to do 
$('div').find('span').end() // to get back at the div level

另一个例子

$('div').find('p').find('span') // <-- you are at the span level
$('div').find('p').find('span').end() // <-- you are now at the p level
$('div').find('p').find('span').end().end() // back at the div level

所以你必须做

  $('#invoiceTable tbody>tr:last').clone(true)
      .insertAfter('#invoiceTable tbody>tr:last').find("input").val("").end() // add end
      .find("select").val("").end() // add end
      .find("checkbox").attr('name', "soldOut[" +rowCount + "]");

另一个错误是这个

.find("checkbox") // <-- there are no checkbox elements

改成这个

.find("input[type=checkbox]") // they are input with type=checkbox

小提琴

于 2013-03-02T17:05:20.840 回答