简体   繁体   中英

firebug and chrome console both have disappearing console.log messages

I am writing my first site from scratch - I have a form and a function that acts when the form is submitted:

application.js

$(document).ready(function() {
    $("#signupform").submit(function(e) {
        var name = document.getElementById("pname").value;
        var email = document.getElementById("email").value;
        var userArray = [];
        var user = {
            name: name,
            email: email
        };
        console.log(user.email, user.name);
        e.preventDefault;
    });
});

The message gets logged to the console correctly...but it is only a blip - it disappears right away. Also...any errors I was getting while writing the above code also only showed up as short blips in the console. Just barely long enough to read.

Here is my index.html file...incase it is relevant:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>it IT</title>
  <link rel="stylesheet" type="text/css" href="application.css" />
  <script src="jquery.js"></script> 
  <script src="application.js"></script>
  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>
<body>
    <h1>it IT</h1>
    <div id="signup">
        <form id="signupform">
            Name: <input type="text" name="pname" id="pname"><br>
            Email: <input type="text" name="email" id="email"><br>
            <input type="submit" value="sign up">
        </form>
    </div>
    <div id="signin"></div>
    <div id="content"></div>
</body>
</html>

preventDefault is a method, you need:

e.preventDefault();

In your question code, the form was submited so console was refreshed.

Actually e.preventDefault is not correct, you need to do this:

$(document).ready(function () {
    $("#signupform").submit(function (e) {
        e.preventDefault();  // Missing () for preventDefault method
        var userArray = [];
        var user = {
            name: $('#pname').val(),  // also, you can get the values here only
            email: $('#email').val()  // no need to use extra variables for it
        };
        console.log(user.email, user.name);
    });
});

In your browser console go to settings (on the top-right corner) and check the preserve log option. That should prevent the page from reloading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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