移至下一个已禁用并启用onchange的输入 [英] Moving to the next input which was disabled and enabled onchange
问题描述
我有一个禁用的元素,只有在值被输入到之前的输入后,才启用它,问题是 change
在输入丢失焦点和焦点移动之前触发到下一个没有禁用的元素,所以删除 disabled
属性是不行的。
HTML: > jQuery: 我为了克服这个通过改变
在之前的输入之后填写:< input id =bdisabled =disabled/>
$ b
$('#a')。change函数(){
if(this.value ===)
$('#b')。attr('disabled','disabled');
else
$('#b')。removeAttr('disabled');
});
disabled
为 readonly
,但是它是只读的而不是被禁用的,和强大的 readonly
?
考虑: 在订阅多个事件时,一个合理的防弹方法是: 下面是一个演示: 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 HTML: jQuery: I manged to overcome this problem by changing from Things to consider:
你已经在你的问题中说过,你不想订阅多个事件,但这是我能想到的唯一方法要做到这一点。问题是不同的方法改变输入的值都直接与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');
});
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.Fill this before you continue: <input id="a" />
Fill this after the previous input: <input id="b" disabled="disabled" />
$('#a').change(function(){
if (this.value === "")
$('#b').attr('disabled', 'disabled');
else
$('#b').removeAttr('disabled');
});
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
?
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屋!