jquery-如何选择特定的表行
我有个问题。我可以动态创建多行。每行有2个jquery-如何选择特定的表行,jquery,tablerow,Jquery,Tablerow,我有个问题。我可以动态创建多行。每行有2个。每个都有类,tdDebit和tdCredit <table id="tblAcctForm"> <tr> <td class="tdDebit"><input type="text" /></td> <td class="tdCredit"><input type="text" /></td> </tr> </table>
。每个
都有类,tdDebit
和tdCredit
<table id="tblAcctForm">
<tr>
<td class="tdDebit"><input type="text" /></td>
<td class="tdCredit"><input type="text" /></td>
</tr>
</table>
请在此处查看我的工作代码:
PS:在测试之前创建多行
请帮忙。提前感谢。传递给
其他元素的值太一般:它选择页面上所有tr中的所有.tdDebit/.tdCredit中的所有输入
而是将与输入元素相关的直接值传递给它:
$(document).ready(function () {
$("tr .tdDebit input").live("keyup", function() {
var toDisable = $(this).closest('tr').find('.tdCredit input');
checkContent(this, toDisable);
});
$("tr .tdCredit input").live("keyup", function() {
var toDisable = $(this).closest('tr').find('.tdDebit input');
checkContent(this, toDisable);
});
});
更好的是,让它更通用:
$(document).ready(function () {
$("tr input").live("keyup", function() {
var toDisable = $(this).closest('tr').find('input').not(this);
checkContent(this, toDisable);
});
});
传递给其他元素的值太笼统:它选择页面上所有tr内的所有输入。tdDebit/.tdCredit在所有tr内
而是将与输入元素相关的直接值传递给它:
$(document).ready(function () {
$("tr .tdDebit input").live("keyup", function() {
var toDisable = $(this).closest('tr').find('.tdCredit input');
checkContent(this, toDisable);
});
$("tr .tdCredit input").live("keyup", function() {
var toDisable = $(this).closest('tr').find('.tdDebit input');
checkContent(this, toDisable);
});
});
更好的是,让它更通用:
$(document).ready(function () {
$("tr input").live("keyup", function() {
var toDisable = $(this).closest('tr').find('input').not(this);
checkContent(this, toDisable);
});
});
这样就可以了。不再需要第二个参数
function checkContent (activeElem, otherElem) {
var $input=$(activeElem), $otherInput=$input.parent().next().find('input');
if ($.trim($input.val()) != "")
$otherInput.attr("disabled", "disabled");
else
$otherInput.removeAttr("disabled");
}
演示:http://jsfiddle.net/pcmwg/1/这样就可以了。不再需要第二个参数
function checkContent (activeElem, otherElem) {
var $input=$(activeElem), $otherInput=$input.parent().next().find('input');
if ($.trim($input.val()) != "")
$otherInput.attr("disabled", "disabled");
else
$otherInput.removeAttr("disabled");
}
演示:http://jsfiddle.net/pcmwg/1/此外,如果您使用的是jQuery 1.7或更高版本,请在
上使用us而不是live
:此外,如果您使用的是jQuery 1.7或更高版本,请在
上使用us而不是live
: