如何使用 javascript 禁用和重新启用按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8394562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:50:12  来源:igfitidea点击:

How do I disable and re-enable a button in with javascript?

javascripthtml

提问by Adam

I can easily disable a javascript button, and it works properly. My issue is that when I try to re-enable that button, it does not re-enable. Here's what I'm doing:

我可以轻松禁用 javascript 按钮,并且它可以正常工作。我的问题是,当我尝试重新启用该按钮时,它不会重新启用。这是我在做什么:

<script type="text/javascript">
    function startCombine(startButton) {

        startButton.disabled = 'true';

        startButton.disabled = 'false';

    }
</script>
<input type='button' id='start' value='Combine Selected Videos'
onclick='startCombine(this);'>

Why isn't this working, and what can I do to make it work?

为什么这不起作用,我该怎么做才能使它起作用?

回答by alex

trueand falseare not meant to be strings in this context.

true并且false在这种情况下并不意味着是字符串。

You want the literal trueand falseBooleanvalues.

你想要文字truefalseBoolean值。

startButton.disabled = true;

startButton.disabled = false;

The reason it sort ofworks (disables the element) is because a non empty string is truthy. So assigning 'false'to the disabledproperty has the same effect of setting it to true.

有点工作(禁用元素)的原因是因为非空字符串是truthy。因此,分配'false'disabled属性与将其设置为true.

回答by debasish

<script>
function checkusers()
{
   var shouldEnable = document.getElementById('checkbox').value == 0;
   document.getElementById('add_button').disabled = shouldEnable;
}
</script>

回答by Narendra

you can try with

你可以试试

document.getElementById('btn').disabled = !this.checked"

document.getElementById('btn').disabled = !this.checked"

<input type="submit" name="btn"  id="btn" value="submit" disabled/>

<input type="checkbox"  onchange="document.getElementById('btn').disabled = !this.checked"/>