在字段中输入文本时需要启用按钮。

时间:2014-03-17 22:03:59

标签: javascript html

当我在文本框中输入文本时,我需要启用我的按钮。我做错了什么?

代码:

<body>
<script>
  function IsEmpty(){ 
    if(input1.value!=null){
      sendbutton.enabled==true;
    }
  }
  IsEmpty();   
</script>

<input class="draft" name="input1" type="text"/> 
     <button class="send" id="sendbutton" disabled>  Send </button> 
     <ul class="messages">
     </ul>
</body>

4 个答案:

答案 0 :(得分:1)

你可能想要sendbutton.enabled=true;,只有一个=。你写的是检查它们是否相等(大概是false),然后对结果没有任何作用。

答案 1 :(得分:1)

JavaScript 更改为:

var input1 = document.getElementById('input1'),
    sendbutton = document.getElementById('sendbutton');
function IsEmpty(){ 
  if (input1.value){
    sendbutton.removeAttribute('disabled');
  } else {
    sendbutton.addAttribute('disabled', '');
  }
}

input1.onkeyup = IsEmpty;

HTML

<input class="draft" id="input1" type="text"/> 
<button class="send" id="sendbutton" disabled>Send</button> 

DEMO

答案 2 :(得分:0)

要修改DOM中的元素,您需要使用纯Javascript的功能,例如getElementById函数或任何其他Javascript框架。

的document.getElementById( 'sendbutton')

然后参考该属性并进行更改。

你也可以使用JQuery来帮助你。我的意思是,使用选择器。

希望有所帮助,

答案 3 :(得分:0)

You need to assign an id to your input element.
<input class="draft" name="input1" id='input1' type="text"/> 

and in your javascript access it like this:

if(document.getElementById('input1').value != null || document.getElementById('input1').value!='undefined'){
  document.getElementById('sendbutton').disabled=false;
}