我有这样的形式:
<form id="myform" name="myform" action="test.php" method="post">
<input type="text" name="shout-in" id="proShoutIn" maxlength="80" />
<a href="#" id="add_shout"><img src="post.gif"/></a>
</form>
我如何做一篇ajax文章,以便使用if (isset($_POST['shout-in'])){..do something..}
?我需要得到在<input>
中输入的值,并用它做一篇文章。
有什么想法吗?感谢
$('#add_shout').click(function () {
var $form=$('#myform');
$.post($form.attr('action'), $form.serialize());
});
- $.post()-post方法的
$.ajax()
简写 - .serialize()-以标准URL编码的表示法创建文本字符串
使用$.post()
的第三个(可选)参数,您可以指定一个回调函数,该函数将接收作为其唯一参数发送回的任何内容。它将在AJAX查询成功完成时运行(因此您可以根据AJAX调用等进行DOM修改)
您可能还想阻止默认表单提交(在许多浏览器中,在输入字段中按Enter键会触发它),并运行AJAX提交:
$('#myform').submit(function (e) {
$('#add_shout').click();
e.preventDefault();
});
$.post("test.php", $("#myform").serialize(),
function(data) {
// do something with the response
}
);
$("#myform").submit(function (e) {
$.post(this.action, $(this).serialize(), function (data) {
//handle response
});
//prevent form from submitting. In jQuery, do not use return false
e.preventDefault();
}
Nettuts-plus:
使用jQuery 提交没有页面刷新的表单