禁用在 HTML 中提交输入



当我尝试此代码时,即使输入中有值,按钮输入也会保持禁用状态,为什么?

<form action ="/buy" method = "POST">
<input id="symbol" type='text' name= 'symbol'/>
<input id="shares" type= 'text' name = 'shares'/>
<input id="submit" type= 'submit' disabled/>
</form>
<script>
document.querySelector('#shares').onkeyup = function(){
if (document.querySelector('#shares').value === '') {
document.querSelector('#submit').disabled = true;
} else {
document.querSelector('#submit').disabled = false;  
}
}
</script>

除了这个语句之外,一切都是正确的 else 中,您的语法有错误

<form action ="/buy" method = "POST">
<input id="symbol" type='text' name= 'symbol'/>
<input id="shares" type= 'text' name = 'shares'/>
<input id="submit" type= 'submit' disabled/>
</form>
<script>
document.querySelector('#shares').onkeyup = function(){
if (document.querySelector('#shares').value === '') {
document.querySelector('#submit').disabled = true;
} else {
document.querySelector('#submit').disabled = false;  
}
}
</script>

工作小提琴:https://jsfiddle.net/akcvtf2r/

最新更新