2013-03-02 51 views
0

當我向表格中插入一個新的表格行時,我需要重命名一個複選框。對於皮特的愛,我不知道爲什麼這不工作用jQuery重命名複選框

$('.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> 

任何想法的問題是什麼?

回答

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 

FIDDLE

+0

似乎並沒有任何區別。 – Casey 2013-03-02 17:11:05

+0

@Casey你可以用生成的html創建一個jsfiddle.net嗎? – 2013-03-02 17:12:51

+0

它是這樣的:http://jsfiddle.net/mSVkp/ – Casey 2013-03-02 17:18:41