简体   繁体   English

jQuery事件仅触发一次,即使多次触发也是如此

[英]JQuery event only fires once, even though it is triggered more than once

Initially, all input fields in the 'invoice-line' containers are disabled except the first one. 最初,“发票行”容器中的所有输入字段均被禁用,但第一个除外。 When a user inputs any text into any of the fields belonging to the first 'invoice-line', I would like for the second invoice-line inputs to become enabled. 当用户将任何文本输入到属于第一个“发票行”的任何字段中时,我希望启用第二个发票行输入。 When a user inserts any text into any of the input fields of the second invoice line I would like for the third invoice line's input fields to become enabled. 当用户在第二张发票行的任何输入字段中插入任​​何文本时,我希望第三张发票行的输入字段处于启用状态。

I worked out some jquery that SHOULD work to accomplish this, and it DOES but only for the first invoice-line. 我设计了一些应该完成此任务的jquery,但确实适用于第一条发票行。 In other words, once the event is triggered once, it is never triggered again. 换句话说,一旦事件被触发一次,就不再被触发。 If it was, my problem would be solved, as my code seems to work correctly but only 'onChange' of the first invoice-line element. 如果是这样,我的问题将得到解决,因为我的代码似乎可以正常工作,但是只有第一个发票行元素的“ onChange”。

I have been trying to solve this one for a long long time, I would greatly appreciate any help with this. 我一直在努力解决这一问题,对此我将不胜感激。

Markup: 标记:

<?php for($i=0; $i < 20; $i++){
    echo '
    <div class="invoice-line">
        <div class="prod-id-cell"><input type="text" class="prod-id-input"></div>
        <div class="prod-name-cell">
            <input onKeyPress="search(this.value)" type="text" class="prod-name-input"/>
            <div class="smart-suggestions">
                    <!-- RESULT SUGGESTIONS WILL POPULATE HERE -->
            </div>
        </div>
        <div class="price-cell"><input class="price-input" type="text" /></div>
        <div class="unit-price-cell"><input class="unit-price-input" type="text" /></div>
        <div class="num-kits-cell"><input class="num-kits-input" type="text" /></div>
        <div class="amount-cell"><input class="amount-input" type="text" /></div>
    </div>';
}
?>

JQuery: JQuery的:

//regulate what lines are enabled / disabled
$('.invoice-line div input').attr('disabled','disabled');
$('.invoice-line:first div input').removeAttr('disabled');

$('.invoice-line div input').change(function(){
    $(this).parent().parent('.invoice-line').next().find('input').removeAttr('disabled');
});

There is one parent() to many, targeting the invoice-line wrapper, then finding the next() invoice-line , and what your probably looking for is really the next div and then the containing input etc : 许多人都有一个parent() ,它针对invoice-line包装器,然后找到next() invoice-line ,您可能要查找的实际上是下一个div,然后包含输入等:

$('.invoice-line input').not(':first').prop('disabled',true)
                        .end().on('keyup', function() {
    $(this).closest('div').next().find('input').prop('disabled', false);
});

FIDDLE FIDDLE

Also changed it to keyup, so as to not need a blur event to trigger the activation of the next input. 也将其更改为keyup,以便不需要模糊事件即可触发下一个输入的激活。

EDIT: 编辑:

according to the comments, maybe this is what you're after ? 根据评论,也许这是您所追求的?

$('.invoice-line').not(':first').find('input').prop('disabled',true);

$('input', '.invoice-line').on('keyup', function() {
    $(this).closest('.invoice-line').next('.invoice-line').find('input').prop('disabled', false);
});

FIDDLE 小提琴

EDIT: 编辑:

Try this one: 试试这个:

$('.invoice-line').not(':first').find('input').prop('disabled',true);

$(document).on('keyup', '.invoice-line input', function() {
    $(this).closest('.invoice-line').next('.invoice-line').find('input').prop('disabled', false);
});

Try with this code: 尝试使用以下代码:

          // first all the inputs in the .invoice-line get disabled
$('.invoice-line input[type="text"]').attr("disabled","disabled");
         // then the first of all in this becomes enabled 
$('.invoice-line input[type="text"]:first').removeAttr('disabled');

         // then on change next input will be enabled for inputing the values
$('.invoice-line input[type="text"]').change(function(){
    $(this).parent().next().find('input').removeAttr('disabled').focus();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM