移至下一个已禁用并启用onchange的输入 [英] Moving to the next input which was disabled and enabled onchange

查看:154
本文介绍了移至下一个已禁用并启用onchange的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个禁用的元素,只有在值被输入到之前的输入后,才启用它,问题是 change 在输入丢失焦点和焦点移动之前触发到下一个没有禁用的元素,所以删除 disabled 属性是不行的。



HTML: >
在之前的输入之后填写:< input id =bdisabled =disabled/>

jQuery:
$ b

  $('#a')。change函数(){
if(this.value ===)
$('#b')。attr('disabled','disabled');
else
$('#b')。removeAttr('disabled');
});



我为了克服这个通过改变 disabled readonly ,但是它是只读的而不是被禁用的,和强大的(鼠标证明)方法来实现它,并使用 readonly



考虑:


  • 订阅多个活动看起来不太合适。 >用户可以在不使用键盘的情况下将文本粘贴到第一个输入。

  • 你已经在你的问题中说过,你不想订阅多个事件,但这是我能想到的唯一方法要做到这一点。问题是不同的方法改变输入的值都直接与C / C ++ DOM接口,但是他们不通过JS API来完成。有关详情,请参见
    此问题。 p>

    在订阅多个事件时,一个合理的防弹方法是:

     <$ c $ ()。$('#a')。on('keyup paste propertychange input',function(){
    if(this.value ===)$('#b')。prop('disabled ',true);
    else $('#b')。removeAttr('disabled');
    });

    下面是一个演示: http://jsfiddle.net/HmzYR/


    I have a disabled element which I enable it only after value was entered to it's previous input, the problem is that change triggers before the input lost focus and the focus moved to the next not disabled element, so removing the disabled attribute then is no good.

    HTML:

    Fill this before you continue:      <input id="a" />
    Fill this after the previous input: <input id="b" disabled="disabled" />​
    

    jQuery:

    $('#a').change(function(){
        if (this.value === "")
            $('#b').attr('disabled', 'disabled');
        else
            $('#b').removeAttr('disabled');
    });​
    

    Fiddle

    I manged to overcome this problem by changing from disabled to readonly but then it's readonly and not disabled as I preferred, are there good and robust(mouse proof) ways to achieve it and using readonly?

    Things to consider:

    • Subscribing to multiple events doesn't really seems right.
    • The user can paste text to the 1st input without using keyboard at all.

    解决方案

    You have said in your question that you don't want to subscribe to multiple events, but this is the only way I can think of to do this. The problem is that different ways of changing the value of the input all interface directly with the C/C++ DOM, but they do not do it through the JS API. See this question for more on this.

    A reasonably bulletproof way of doing it while subscribing to multiple events would be:

    $('#a').on('keyup paste propertychange input', function() {
        if (this.value === "") $('#b').prop('disabled', true);
        else $('#b').removeAttr('disabled');
    });
    

    Here is a demonstration: http://jsfiddle.net/HmzYR/

    这篇关于移至下一个已禁用并启用onchange的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆