Javascript login form doesn't submit when user hits Enter

后端 未结 7 1285
独厮守ぢ
独厮守ぢ 2020-12-31 10:06

I\'m working on a simple javascript login for a site, and have come up with this:

Login to view!
相关标签:
7条回答
  • 2020-12-31 10:45

    There are several topics being discussed at once here. Let's try to clarify.

    1. Your Immediate Concern:

    (Why won't the input button work when ENTER is pressed?)

    Use the submit button type.

    <input type="submit".../> 
    

    ..instead of

    <input type="button".../>
    

    Your problem doesn't really have anything to do with having used an onclick attribute. Instead, you're not getting the behavior you want because you've used the button input type, which simply doesn't behave the same way that submit buttons do.

    In HTML and XHTML, there are default behaviors for certain elements. Input buttons on forms are often of type "submit". In most browsers, "submit" buttons fire by default when ENTER is pressed from a focused element in the same form element. The "button" input type does not. If you'd like to take advantage of that default behavior, you can change your input type to "submit".

    For example:

    <form action="/post.php" method="post">
        <!-- 
        ...
        -->
        <input type="submit" value="go"/>
    </form>
    

    2. Security concerns:

    @Ady mentioned a security concern. There are a whole bucket of security concerns associated with doing a login in javascript. These are probably outside of the domain of this question, especially since you've indicated that you aren't particularly worried about it, and the fact that your login method was actually just setting the location.href to a new html page (indicating that you probably don't have any real security mechanism in place).

    Instead of drudging that up, here are links to related topics on SO, if anyone is interested in those questions directly.

    • Is there some way I can do a user validation client-side?
    • Encrypting Ajax calls for authentication in jQuery

    3. Other Issues:

    Here's a quick cleanup of your code, which just follows some best practices. It doesn't address the security concern that folks have mentioned. Instead, I'm including it simply to illustrate some healthy habits. If you have specific questions about why I've written something a certain way, feel free to ask. Also, browse the stack for related topics (as your question may have already been discussed here).

    The main thing to notice is the removal of the event attributes (onclick="", onsubmit="", or onkeypress="") from the HTML. Those belong in javascript, and it's considered a best practice to keep the javascript events out of the markup.

    <form action="#" method="post" id="loginwindow">
        <h3>Login to view!</h3>
        <label>User ID: <input type="text" id="userid"></label>
        <label>Password: <input type="password" id="pass"></label>
        <input type="submit" value="Check In" />
    </form>
    
    <script type="text/javascript">
    window.onload = function () {
        var loginForm = document.getElementById('loginwindow');
        if ( loginwindow ) {
            loginwindow.onsubmit = function () {
    
                var userid = document.getElementById('userid');
                var pass = document.getElementById('pass');
    
                // Make sure javascript found the nodes:
                if (!userid || !pass ) {
                    return false;
                }
    
                // Actually check values, however you'd like this to be done:
                if (pass.value !== "secret")  {
                    location.href = 'failure.html';
                }
    
                location.href = 'album.html';
                return false;
            };
        }
    };
    </script>
    
    0 讨论(0)
  • 2020-12-31 10:52

    Maybe you can try this:

    <form id="loginwindow" onsubmit='validate(text2.value,"username",text1.value,"password")'>
    <strong>Login to view!</strong>
    <p><strong>User ID:</strong>
       <input type="text" name="text2">
    </p>
    <p><strong>Password:</strong>
    <input type="password" name="text1"><br>
       <input type="submit" value="Check In"/>
    </p>
    
    </form>
    

    As others have pointed out, there are other problems with your solution. But this should answer your question.

    0 讨论(0)
  • 2020-12-31 10:52

    My Thought = Massive security hole. Anyone can view the username and password.

    More relevant to your question: - You have two events happening.

    1. User clicks button.
    2. User presses enter.

    The enter key submits the form, but does not click the button.

    By placing your code in the onsubmit method of the form the code will run when the form is submitted. By changing the input type of the button to submit, the button will submit the form in the same way that the enter button does.

    Your code will then run for both events.

    0 讨论(0)
  • 2020-12-31 11:00

    Put the script directly in your html document. Change the onclick value with the function you want to use. The script in the html will tell the form to submit when the user hits enter or press the submit button.

     <form id="Form-v2" action="#">
    
    <input type="text" name="search_field"  placeholder="Enter a movie" value="" 
    id="search_field" title="Enter a movie here" class="blink search-field"  />
    <input type="submit" onclick="" value="GO!" class="search-button" />        
     </form>
    
        <script>
        //submit the form
        $( "#Form-v2" ).submit(function( event ) {
          event.preventDefault();
        });
             </script>
    
    0 讨论(0)
  • 2020-12-31 11:03

    Surely this is too unsecure as everyone can crack it in a second ...

    -- only pseudo-secure way to do js-logins are the like:

    <form action="http://www.mySite.com/" method="post" onsubmit="this.action+=this.theName.value+this.thePassword.value;">
      Name: <input type="text" name="theName"><br>
      Password: <input type="password" name="thePassword"><br>
      <input type="submit" value="Login now">
    </form>
    
    0 讨论(0)
  • 2020-12-31 11:08

    Instead of <input type="button">, use <input type="submit">. You can put your validation code in your form onsubmit handler:

    <form id="loginwindow" onsubmit="validate(...)">

    0 讨论(0)
提交回复
热议问题