在html中禁用提交输入 [关闭]

问题描述 投票:0回答:1

当我尝试这段代码时,即使输入中有一个值,按钮输入也会被禁用,为什么?

<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>
javascript html disabled-input
1个回答
0
投票

除了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.netakcvtf2r

© www.soinside.com 2019 - 2024. All rights reserved.