[英]how to use if statment inside $.ajax( and check for string response?
I am trying to do this simple compare inside ajax post request. 我想在ajax post请求中做这个简单的比较。 I just want to check the post response for different string values but it never works.
我只是想检查不同字符串值的帖子响应,但它永远不会工作。 The popup function never get called even i know for sure that php response is string "mango"could you guys tell me what i am doing wrong here ?
弹出功能永远不会被调用,即使我知道php响应是字符串“mango”你能告诉我我在这里做错了吗?
$.ajax(
{
type: 'POST',
url: './doit1.php',
data: {
title: 'test',
},
success: function (good)
{
//handle success
if($(good) =='mango')
{
popitup('./doit1.php?action=stop')
//alert(good)
else if ($(good) =='orange')
{
//do something
}
else
{
//default action
}
},
failure: function (bad)
{
//handle any errors
alert(bad)
}
});
function popitup(url) {
newwindow=window.open(url,'name','height=200,width=150');
if (window.focus) {newwindow.focus()}
return false;
}
You are trying to wrap a jQuery wrapper around your returned data here: 您正试图在返回的数据周围包装一个jQuery包装器:
if($(good) =='mango')
That is incorrect, as good
would need to be a DOM element in that case for this to have any meaning. 这是不正确的,因为在这种情况下
good
将需要是DOM元素,因为这具有任何意义。
If good
is just a string value that is returned, you should just be doing this: 如果
good
只是返回的字符串值,那么您应该这样做:
if(good == 'mango')
You're wrapping good in jQuery when it should just be put in as a string. 当它应该只是作为一个字符串放入时,你在jQuery中包装得很好。
Replace the success function with. 替换成功功能。
success: function (good){
//handle success
if(good==="mango"){
popitup('./doit1.php?action=stop')
}else if(good==="orange"){
//Do something
}else{
//Default Action
}
}
Without knowing what good
is it's hard to say. 不知道有什么
good
,很难说。 However, $()
always returns a jQuery collection object that is no good for comparisons of any kind. 但是,
$()
总是返回一个jQuery集合对象,这对任何类型的比较都没有好处。 You probably just want to use good == 'mango'
or a switch
. 你可能只想使用
good == 'mango'
或switch
。 Perhaps it's good.fruit == 'mango'
. 也许这
good.fruit == 'mango'
。 good.fruit == 'mango'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.