禁用“提交”按钮,除非选择了表单选项

时间:2016-06-08 12:56:21

标签: html forms dropdown

我已经在线检查了几页如何禁用下拉框的提交按钮,除非选择了一个选项。但找不到适合我的方法。 任何有关如何做到这一点的帮助都会很棒!

修改:我不希望<option value="not_valid">-- select an option --</option>被视为选项

&#13;
&#13;
<form action="action_page.php">
  <select name="attacks" id="attacks">
    <option value="not_valid">-- select an option --</option>
    <option value="attack1">1</option>
    <option value="attack2">2</option>
    <option value="attack3">3</option>
    <option value="attack4">4</option>
    <option value="attack5">5</option>
  </select>
  <br>
  <input type="button" id="battle" onclick="game()" value="Battle">
</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

使用onChange事件

<html>
  <head>

  </head>

  <body>
    <form action="action_page.php">
      <select name="attacks" id="attacks" onchange="selectChanged()">
        <option value="not_valid">-- select an option --</option>
        <option value="attack1">1</option>
        <option value="attack2">2</option>
        <option value="attack3">3</option>
        <option value="attack4">4</option>
        <option value="attack5">5</option>
      </select>
      <br>
      <input type="button" id="battle" onclick="game()"  value="Battle">
    </form>
  <script>
 function selectChanged(){
        attacks = document.getElementById("attacks");
        if(attacks.value =="not_valid"){//or whatever th unwanted value is
            document.getElementById("battle").disabled = true;
        } else{
            document.getElementById("battle").disabled = false; 
        }
    }          
  </script>
  </body>
  </html>