当我尝试这段代码时,即使输入中有一个值,按钮输入也会被禁用,为什么?
<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>