最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable submit button on page load, enable it after function is called - Stack Overflow

programmeradmin0浏览0评论

I have a username/password form where the password appears after the user enters their username. Here is the 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():

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 test1 so 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.

I have a username/password form where the password appears after the user enters their username. Here is the 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():

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 test1 so 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.

Share Improve this question asked Feb 24, 2014 at 23:22 kellysankellysan 131 gold badge1 silver badge3 bronze badges 1
  • 1 Instead of disabling it on page load couldn't you just put the disabled attribute on the button and use your function to enable it. – rfoo Commented Feb 24, 2014 at 23:25
Add a ment  | 

1 Answer 1

Reset to default 5

If you just want to enable/disable input:

$("#your_input").attr('disabled','disabled'); // disable
$("#your_input").removeAttr('disabled');//enable

Example:

http://jsfiddle/juvian/R95qu

发布评论

评论列表(0)

  1. 暂无评论