如何根据变量的值启用或禁用按钮?

时间:2016-02-24 09:48:42

标签: javascript html

我使用纯JavaScript。 我的asp页面中有这一行:

<input type="button" value="..." id="ffcolorswtach" style="width: 20px; height: 19px; position: relative; left: 15px; top: 0px; color: #ff0000; background-color: #d8d5d5;" onclick="PickColor(1, false, false)" role="button" />

此外,我的客户端页面中包含javascript变量,其中包含true / false值:

var isColorSelected

当isColorSelected为false时,我想启用上面的输入元素,如果为false我想禁用它。任何想法我该如何实现它?

4 个答案:

答案 0 :(得分:1)

尝试以下内容:

    if(isColorSelected != true)
      document.getElementById("ffcolorswtach").disabled = true;
    else
      document.getElementById("ffcolorswtach").disabled = false;

这是在DOM中找到该元素并使用内置的浏览器功能来为您禁用该元素。

答案 1 :(得分:0)

通常,您需要disabled属性。:

document.getElementById("ffcolorswtach").disabled = true;

您可以在变量发生变化的同时将其设置为true / false

答案 2 :(得分:0)

HTML DOM禁用了属性 - http://www.w3schools.com/jsref/prop_html_disabled.asp

var isColorSelected = true;
document.getElementById("ffcolorswtach").disabled = isColorSelected;
<input type="button" value="..." id="ffcolorswtach" style="width: 20px; height: 19px; position: relative; left: 15px; top: 0px; color: #ff0000; background-color: #d8d5d5;" onclick="PickColor(1, false, false)" role="button" />

答案 3 :(得分:0)

使用Jquery

if(isColorSelected==true)
  $('#ffcolorswtach').prop('disabled', false);
else
  $('#ffcolorswtach').prop('disabled', true);