jQuery表单处理麻烦

时间:2018-03-16 08:23:57

标签: javascript jquery html

使用jQuery从html表单处理数据时遇到问题。我有这个简单的形式:

<form id="singup">
  <input type="text" placeholder="Full Name" id="signup-name">
  <input type="email" placeholder="Email Address" id="signup-email">
  <input type="password" placeholder="Password" id="signup-pass">
  <input type="password" placeholder="Retype Password" id="signup-retype-pass">
  <button class="button-hover" type="submit" id="signup-submit">Create account now</button>
</form>

现在我想用jQuery获取输入的值。

$("#singup").submit(function (e) {
    var email = $('#singup-email').val();
    console.log(email);
    e.preventDefault(e);
});

但浏览器控制台告诉我var email未定义。为什么呢?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

错字错误。使用signup-email而不是singup-email

$("#singup").submit(function (e) {
    var email = $('#signup-email').val();
    console.log(email);
    e.preventDefault(e);
});

答案 1 :(得分:1)

$("#signup-submit").on('click', function (e) {
    var email = $('#signup-email').val();
    console.log(email);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="singup">
  <input type="text" placeholder="Full Name" id="signup-name">
  <input type="email" placeholder="Email Address" id="signup-email">
  <input type="password" placeholder="Password" id="signup-pass">
  <input type="password" placeholder="Retype Password" id="signup-retype-pass">
  <button class="button-hover" type="button" id="signup-submit">Create account now</button>
</form>