jQuery Ajax成功:function() [英] JQuery Ajax success: function()
问题描述
我将数据发布到php处理页面,如下所示:
I post data to a php processing page like this:
$insert = mysql_query(
'INSERT INTO ' . $table . ' (' . substr($addfields,0,-1) . ') ' .
'VALUES (' . substr($addvals,0,-1) . ')');
我想要拥有:
if($insert): echo 'Message 1'; else: echo 'message2'; endif;
要成功,我该怎么做:function()以在<div id="result"></div>
中显示消息?
What do I do in my success: function() to display the message in <div id="result"></div>
?
我尝试过:
success: function() {
$(#result).html(html);
}
该代码不会在div标签中显示消息.
That code doesn't display the message in the div tag.
如何将数据发布到div?
How do I post data to the div?
推荐答案
假设您的 $.ajax()
请求正在使用dataType: 'html'
(或默认值,它将默认猜测),您的success
函数将接收返回的文本作为其第一个参数:
Assuming that your $.ajax()
request is using dataType: 'html'
(or the default, which will intelligently guess) your success
function will receive the returned text as its first parameter:
$.ajax({
url: '/mypage.php',
dataType: 'html',
success: function(data) {
$('#result').html(data);
}
});
应该使用您的mypage.php
转储的HTML,然后替换页面上<div id="result">
的内容!
Should take whatever HTML your mypage.php
dumps out, and replace the contents of the <div id="result">
on the page just fine!
我已经组装了一个 jsfiddle演示供您查看.
I have assembled a jsfiddle demo for you to look at.
这篇关于jQuery Ajax成功:function()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!