简体   繁体   English

页面加载时如何使“提交”按钮保持禁用状态

[英]How to make Submit button stay disable when page loads

Ok so I've managed to do most of what I want to do with this form, but missing one thing from the puzzle. 好的,我已经设法完成了此表单的大部分操作,但是却从拼图中遗漏了一件东西。 I have a dropdown form which requires you to select a number of different options. 我有一个下拉列表,要求您选择许多不同的选项。 However, I have one option, which is what is shown when the page loads "-- select an option --", that disables the submit button when it is selected, but it does not disable it when the page loads, which is the option that is automatically selected, if that makes sense. 但是,我有一个选项,即页面加载“-选择一个选项-”时显示的选项,它在选中时禁用了提交按钮,但是在页面加载时却不禁用它。如果可以的话,自动选择的选项。 How do I automatically make the submit button disabled when the page loads? 页面加载后如何自动禁用“提交”按钮?

 function selectChanged() { attacks = document.getElementById("attacks"); if (attacks.value == "not_valid") { document.getElementById("sbattle").disabled = true; } else { document.getElementById("sbattle").disabled = false; } } 
 <form> <select name="attacks" id="attacks" value="Select" onchange="selectChanged()"> <option value="not_valid">-- select an option --</option> <option value="example1">example 1</option> <option value="example2">example 2</option> <option value="example3">example 3</option> <option value="example4">example 4</option> <option value="example5">example 5</option> </select> <br> <input type="button" id="sbattle" onclick="sBattle()" value="Battle"> </form> 

disabled添加到您的HTML

<input type="button" id="sbattle" onclick="sBattle()" value="Battle" disabled="disabled">

Call selectChanged() on your page load event... or better yet on dom ready. 在您的页面加载事件上调用selectChanged() ,或者在准备好dom之后调用selectChanged()

see: https://jsfiddle.net/8eh1qcdb/ 参见: https : //jsfiddle.net/8eh1qcdb/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM