我有一种形式,其中我有2个文本字段debit_amount和credit_amount。现在,如果我在credit_amount中输入任何值,我想让脚本清除debit_amount字段,反之亦然。两个文本字段在同一行中。这是我的代码段:
<td class="ta-right">
<%= text_field_tag "tablel[line_items_attributes][#{index}][amount]", line_item.amount, :size =>4, :id => "amount", :maxlength => 18, :onkeydown => "return numbersOnly(event);", :class=>"bg-focus form-control text-right"%>
</td>
<td class="ta-right">
<%= text_field_tag "tablel[line_items_attributes][#{index}][credit_amount]", line_item.amount, :size =>4, :id => "amount", :maxlength => 18, :onkeydown => "return numbersOnly(event);", :class=>"bg-focus form-control text-right"%></td>
在脚本文件中,我写了:
var i = 0;
$('table#line_items tbody tr').each(function(){
if (($('table#line_items tbody tr:eq(' + i + ')').is(":visible")) && $('table#line_items tbody tr:eq(' + i + ') #amount').val() && $('table#line_items tbody tr:eq(' + i + ') #credit_amount').val()) {
$('#amount').focusout(function(){
alert('debit');
$('table#line_items tbody tr:eq(' + i + ') #amount').val(' ');
});
$('#credit_amount').focusout(function(){
alert('credit');
$('table#line_items tbody tr:eq(' + i + ') #credit_amount').val(' ');
});
}
i++;
});
任何帮助,将不胜感激
我发现了许多问题。首先,您需要为您的credit_amount
“ amount”和“ amount”元素使用类而不是id 。
假设将它们更改为具有类,则不需要该i
值(与使用该事件的事件相比,该值是全局的,因此它将不起作用)。
使用一些真实的HTML尝试这样的事情(如果有的话):http : //jsfiddle.net/TrueBlueAussie/PEV66/1/
$(function () {
$('table#line_items tbody tr').each(function () {
var $tr = $(this);
var $amt = $tr.find('.amount');
var $credit = $tr.find('.credit_amount');
if ($tr.is(":visible") && $amt.val() && $credit.val())
{
$amt.focusout(function() {
$(this).val(' ');
alert('debit');
}
$credit.focusout(function () {
$(this).val(' ');
alert('credit');
});
}
});
});
如果此清理操作做得还不够,您将需要阐明预期的行为。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句