选中复选框时,jquery +填充同一行中的字段

时间:2013-06-10 10:37:37

标签: jquery

我有一个要求,就是在检查一个方框时,用一个值填充同一行的输入。

我的代码是:

function populatetransportprice() {
// iterate through the "checked" checkboxes
$("table.authors-list").find('input[type="checkbox"][name^="treated"]:checked').each(function () {
    alert(treatedtransportcostperton);
    row.find('input[name^="transportprice"]').val(treatedtransportcostperton.toFixed(2));
});      

}

每行的输入字段为tranpsortpriceX,其中X为行号。

我的HTML是:

<table class="authors-list" id="ordertable">
<tr>
 <td><input name="transportprice1" id="transportprice1" class="rounded"></td>
</tr>
<tr>
 <td><input name="transportprice2" id="transportprice2" class="rounded"></td>
</tr>
<tr>
 <td><input name="transportprice3" id="transportprice3" class="rounded"></td>
</tr>
</table>

警告会为表中的每个复选框填充,但不会填充输入。 我假设我的row.find

任何建议表示赞赏, 感谢

更新

当前语法:

 function populatetransportprice() {
$("table.authors-list").find('input[type="checkbox"][name^="treated"]:checked').each(function () {
 $(this).find('input[name^="transportprice"]').val(treatedtransportcostperton.toFixed(2));
$(this).next('input[name^="transportprice"]').val(treatedtransportcostperton.toFixed(2));
});    
}

2 个答案:

答案 0 :(得分:1)

'row'没有在任何地方定义。

    $("table.authors-list").find('input[type="checkbox"][name^="treated"]:checked').each(function () {
        alert(treatedtransportcostperton);
//Try $(this) instead of row. Here 'this' implies each and every element in the loop and then it finds within the element. I think the following code might help,
     $(this).find('input[name^="transportprice"]').val(treatedtransportcostperton.toFixed(2));
$(this).next('input[name^="transportprice"]').val(treatedtransportcostperton.toFixed(2));
//the above is an assumption. Post your HTML
    });   

答案 1 :(得分:1)

如果没有看到这种影响的标记,很难给出准确的答案。

但是,根据您提供给我们的有限详细信息,我希望这不起作用的原因是您的row变量。

我们没有其他代码显示如何设置,但我怀疑row未正确设置,或者没有引用您认为的元素。< / p>

您最好的选择是在row内设置each,这样您就可以确保定位与输入相关的行。

类似的东西:

function populatetransportprice() {
// iterate through the "checked" checkboxes
$("table.authors-list").find('input[type="checkbox"][name^="treated"]:checked').each(function () {
    var row = $(this).closest('tr'),
        priceInput = row.find('input[name^="transportprice"]');

    alert(treatedtransportcostperton);
    priceInput.val(treatedtransportcostperton.toFixed(2));
}); 

这假设“行”确实是表格行tr

相关问题