2013-03-02 69 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