2009-12-20 86 views
1

我有一个ajax查询发送到php脚本并将响应放入div。代码如下:如何使用jQuery检查字符串的php响应

$.ajax({ 
type: "POST", 
url: "core/process.php", 
data: data, 
success: function(response){ 
    $("#form").fadeOut('slow'); 
    alert("DO NOT LEAVE THIS PAGE UNTIL COMPLETE"); 
    $("#response").html("DO NOT LEAVE THIS PAGE UNTIL COMPLETE<br />") 
    .append(response) 
    .hide() 
    .slideDown('slow'); 
} 
}); 

我想要做的就是检查字符串的响应或返回如“真”或“成功”或什么的,并提醒用户脚本完成。除了你在上面看到的东西之外,我对jQuery知之甚少,我不知道如何搜索某个特定事件的响应。

编辑:我刚刚意识到我忘记了脚本如何输出响应。它只是在PHP脚本中回显。我想我可能需要将所有内容放入数组中,然后json_encode,但这只是一个猜测。

回答

2

您可以使用indexOf功能:

if (response.indexOf('success') > -1 || response.indexOf('true') > -1) { 
    // the response contains one or both of the strings success and true 
} 
+0

这工作完美。谢谢。 – RedElement 2009-12-20 09:09:30

0
if (/Success/.test(response)) { 
    alert('Cool') 
} 

或更改响应类型为JSON,并有服务器编码状态代码和输出字符串作为结果对象。