我正在使用jQuery编写一些功能,我通常通过页面刷新/表单提交来处理这些功能。
基本上,我正在使用jQuery的ajax提交一个表单。
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#show-comment-form").click(function(event){
var ret = $.post("post.php", $("#comment-form").serialize(), "", "html");
$("#theComment").html("<strong>" + ret +"</strong>");
});
});
</script>
</head>
<body>
Testing Stuff
<br /><a href="#" id="show-comment-form">Insert New Message</a><br />
<div id="comment-form">
<form name="form" id="form">
<span>Enter Your Question Here</span>
<textarea id="comment" name="comment"></textarea>
</form>
</div>
<p id="theComment">
avsailable
</p>
</body>
</html>返回的对象是一个XMLHttpRequest。我是操纵这个对象,还是我可能不需要它。
我的问题是,我想让用户使用网页上的表单发布响应,但不需要重新加载页面,并在所有其他用户响应的开头添加html。表单发布到的页面会打印出一些超文本标记语言,但我无法将这些超文本标记语言显示在页面上,因为响应是一个XMLHttpRequest对象。我有点迷路了。谁能给我指个方向?
发布于 2009-07-26 06:28:42
您必须使用callback函数参数:
$("#show-comment-form").click(function(event){
$.post("post.php", $("#comment-form").serialize(), function(ret){
$("#theComment").html("<strong>" + ret +"</strong>");
}, "html");
});该函数将在异步请求结束时执行,其第一个参数是从服务器返回的数据。
发布于 2009-07-26 06:28:10
指定一个函数作为第三个参数,数据应该在回调中返回:
var ret = $.post("post.php",$("#comment-form").serialize(),function(data) { $(data).appendTo('body');});
您不应该需要与XHR对象交互。有关详细信息,请访问:
http://docs.jquery.com/Ajax/jQuery.post
https://stackoverflow.com/questions/1183893
复制相似问题