Dav*_*vid 4 javascript jquery json push
如何使用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(); }
});
}
Run Code Online (Sandbox Code Playgroud)
基本上我需要一种方法来查看d.failed是否存在,如果它='true'则不要继续循环推送.
bea*_*mit 11
你不需要jQuery,只需要JavaScript.您可以通过以下几种方式实现:
typeof d.failed- 返回类型('undefined','Number'等)d.hasOwnProperty('failed')- 以防它继承'failed' in d- 检查是否已设置(即使未定义)您还可以检查d.failed : if (d.failed),但如果d.failed未定义,null,false或零,则返回false.为了简单起见,为什么不if (d.failed === 'true')呢?为什么检查它是否存在?如果是真的,只需返回或设置某种布尔值.
参考:
http://www.nczonline.net/blog/2010/07/27/determining-if-an-object-property-exists/
| 归档时间: |
|
| 查看次数: |
14808 次 |
| 最近记录: |