jQuery - 获取 ajax POST 的表单值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7426085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - Getting form values for ajax POST
提问by user547794
I am trying to post form values via AJAX to a php file. How do I collect my form values to send inside of the "data" parameter?
我正在尝试通过 AJAX 将表单值发布到 php 文件。如何收集我的表单值以在“数据”参数内部发送?
$.ajax({
type: "POST",
data: "submit=1&username="+username+"&email="+email+"&password="+password+"&passconf="+passconf,
url: "http://rt.ja.com/includes/register.php",
success: function(data)
{
//alert(data);
$('#userError').html(data);
$("#userError").html(userChar);
$("#userError").html(userTaken);
}
});
HTML:
HTML:
<div id="border">
<form action="/" id="registerSubmit">
<div id="userError"></div>
Username: <input type="text" name="username" id="username" size="10"/><br>
<div id="emailError" ></div>
Email: <input type="text" name="email" size="10" id="email"/><br>
<div id="passError" ></div>
Password: <input type="password" name="password" size="10" id="password"/><br>
<div id="passConfError" ></div>
Confirm Password: <input type="password" name="passconf" size="10" id="passconf"/><br>
<input type="submit" name="submit" value="Register" />
</form>
</div>
回答by Robin
Use the serialize method:
使用序列化方法:
$.ajax({
...
data: $("#registerSubmit").serialize(),
...
})
Docs: serialize()
文档:序列化()
回答by Ritchie
$("#registerSubmit").serialize() // returns all the data in your form
$.ajax({
type: "POST",
url: 'your url',
data: $("#registerSubmit").serialize(),
success: function() {
//success message mybe...
}
});
回答by Headshota
you can use val function to collect data from inputs:
您可以使用 val 函数从输入中收集数据:
jQuery("#myInput1").val();
回答by Baz1nga
var data={
userName: $('#userName').val(),
email: $('#email').val(),
//add other properties similarly
}
and
和
$.ajax({
type: "POST",
url: "http://rt.ja.com/includes/register.php?submit=1",
data: data
success: function(html)
{
//alert(html);
$('#userError').html(html);
$("#userError").html(userChar);
$("#userError").html(userTaken);
}
});
You dont have to bother about anything else. jquery will handle the serialization etc. also you can append the submit query string parameter submit=1 into the data json object.
您不必担心其他任何事情。jquery 将处理序列化等。您也可以将提交查询字符串参数 submit=1 附加到数据 json 对象中。
回答by Kanishka Panamaldeniya
var username = $('#username').val();
var email= $('#email').val();
var password= $('#password').val();
回答by Chandresh M
try as this code.
试试这个代码。
$.ajax({
type: "POST",
url: "http://rt.ja.com/includes/register.php?submit=1",
data: "username="+username+"&email="+email+"&password="+password+"&passconf="+passconf,
success: function(html)
{
//alert(html);
$('#userError').html(html);
$("#userError").html(userChar);
$("#userError").html(userTaken);
}
});
i think this will work definitely..
我认为这肯定会奏效..
you can also use .serialize()function for sending data via jquery Ajax..
您还可以使用.serialize()函数通过 jquery Ajax 发送数据..
i.e: data : $("#registerSubmit").serialize()
Thanks.
谢谢。