<input type = "text" id = "Height">
<button type = "button" id = "Work!" disabled = "true"> Work! </button>
<script>
var Height = document.getElementById("Height");
var Button = document.getElementById("Work!");
Height.addEventListener("input",function() {
if(Height.value!= "") {
Button.disabled = false;
} else {
Button.disabled = false;
}
});
我不明白为什么当用户在输入字段中输入值时该按钮永远不会启用。我花了两个多小时尝试了所有可能的组合。目前我的盐含量已经超出了极限,任何帮助将不胜感激。
最佳答案
当用户在输入字段中输入值时,按钮将启用。
<script>
function toggleButton(ref,bttnID){
document.getElementById(bttnID).disabled= ((ref.value !== ref.defaultValue) ? false : true);
}
</script>
<form action="" method="post" id="subscribe" name="subscribe">
<label>Height:</label>
<input type="text" name="height" onkeyup="toggleButton(this,'bttnsubmit');">
<input type="button" name="btnsubmit" value="Subscribe" disabled='disabled' id='bttnsubmit' />
</form>
关于javascript - 到底如何使用javascript dom事件---addEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814752/