javascript 在页面加载时禁用提交按钮,在调用函数后启用它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22001305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable submit button on page load, enable it after function is called
提问by kellysan
I have a username/password form where the password appears after the user enters their username. Here is the HTML:
我有一个用户名/密码表单,在用户输入用户名后密码出现。这是 HTML:
<form action="login.php" method="post">
<!-- Username form -->
<div id="form1">
<input type="text" id="username" name="username" placeholder="username">
<input type="button" value="" id="test1" onClick="switchForm()">
</div>
<!-- Password form -->
<div id="form2">
<input type="password" id="password" name="password" placeholder="password">
<input id="button" type="submit" value="">
</form>
and here is the Javascript for the function switchForm():
这是函数 switchForm() 的 Javascript:
function switchForm() {
document.getElementById("form1").style.display = "none";
document.getElementById("form2").style.display = "block";
document.getElementById("password").focus();
}
I have another piece of code that has the enter button simulate a click of test1so the form switches when a user presses enter. The issue is the form submits all the way (including the blank password field). What I want is to disable the submit button during page load, and re-enable it during the execution of switchForm(). Thanks in advance! I think it has something to do with .prop() or .attr() or something, but I'm not sure the best way to do this.
我还有一段代码,它的输入按钮模拟了test1的点击,因此当用户按下输入键时表单会切换。问题是表单一直提交(包括空白密码字段)。我想要的是在页面加载期间禁用提交按钮,并在执行 switchForm() 期间重新启用它。提前致谢!我认为它与 .prop() 或 .attr() 或其他东西有关,但我不确定这样做的最佳方法。
回答by juvian
If you just want to enable/disable input:
如果您只想启用/禁用输入:
$("#your_input").attr('disabled','disabled'); // disable
$("#your_input").removeAttr('disabled');//enable
Example:
例子: