jquery检查json变量是否存在
如何使用 jquery 检查 getJSON 之后生成的 json 中是否存在键/值?
function myPush(){
$.getJSON("client.php?action=listen",function(d){
d.chat_msg = d.chat_msg.replace(/\\\"/g, "\"");
$('#display').prepend(d.chat_msg+'<br />');
if(d.failed != 'true'){ myPush(); }
});
}
基本上我需要一种方法来查看 d.failed 是否存在,如果它=“true”,则不要继续循环推送。
How can I with jquery check to see if a key/value exist in the resulting json after a getJSON?
function myPush(){
$.getJSON("client.php?action=listen",function(d){
d.chat_msg = d.chat_msg.replace(/\\\"/g, "\"");
$('#display').prepend(d.chat_msg+'<br />');
if(d.failed != 'true'){ myPush(); }
});
}
Basically I need a way to see if d.failed exist and if it = 'true' then do not continue looping pushes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为此,您不需要 jQuery,只需要 JavaScript。您可以通过几种方式做到这一点:
typeof d.failed
- 返回类型('undefined'、'Number' 等)d.hasOwnProperty('failed')
-以防万一它您还可以检查 d.failed:
if (d.failed)
,但如果 d.failed 未定义、null、false 或零,则返回 false。为了简单起见,为什么不这样做if (d.failed === 'true')
呢?为什么要检查它是否存在?如果为真,则返回或设置某种布尔值。参考:
http://www. nczonline.net/blog/2010/07/27/确定-if-an-object-property-exists/
You don't need jQuery for this, just JavaScript. You can do it a few ways:
typeof d.failed
- returns the type ('undefined', 'Number', etc)d.hasOwnProperty('failed')
- just in case it's inherited'failed' in d
- check if it was ever set (even to undefined)You can also do a check on d.failed:
if (d.failed)
, but this will return false if d.failed is undefined, null, false, or zero. To keep it simple, why not doif (d.failed === 'true')
? Why check if it exists? If it's true, just return or set some kind of boolean.Reference:
http://www.nczonline.net/blog/2010/07/27/determining-if-an-object-property-exists/