[英]Can't serialize and grab data from form using JavaScript and jQuery
I'm trying to create a simple form where I can add an item, hit submit and grab the data using serialize or .val() but I can't even console log anything once I press submit, what am I doing wrong? 我正在尝试创建一个简单的表单,可以在其中添加项目,单击提交并使用序列化或.val()来获取数据,但是一旦按提交,我什至无法控制台任何日志,我在做什么错? I would just like to be able to grab the data. 我只想能够获取数据。
HTML CODE: HTML代码:
<form >
<input type="text">
<input type="submit">
</form>
JAVASCRIPT CODE: JAVASCRIPT代码:
$(function(){
$("submit").submit(function(){
var data = $("form").serialize();
console.log(data);
});
});
Change your selector $("submit")
to $("form")
and give name attribute to input tag . 将选择器$("submit")
更改为$("form")
并将name属性赋予input tag。 Also add event.preventDefault()
to prevent default form action 还添加event.preventDefault()
以防止默认表单操作
$(function() { $("form").submit(function(e) { e.preventDefault(); var data = $("form").serialize(); console.log(data); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <form> <input name="text" type="text"> <input type="submit"> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.