jQuery 检查返回的数据对象是否为空
$("#post").live("click",function()
{
$("input:checkbox[name='bookmarkid']:checked").each(function()
{
$.post("php/socialbookmark-post.php", {bookmarkID: $(this).val()},function(data)
{
if(data != "") alert(data);
});
});
});
仅当出现问题时,php 文件才会输出一些文本。如果数据为空,我所做的检查将失败并显示一条空消息。我需要解决这个问题。有什么想法吗?
$("#post").live("click",function()
{
$("input:checkbox[name='bookmarkid']:checked").each(function()
{
$.post("php/socialbookmark-post.php", {bookmarkID: $(this).val()},function(data)
{
if(data != "") alert(data);
});
});
});
the php file outputs some text only if something goes wrong. The checking that I do fails if the data is empty and displays an empty message. I need to fix this. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以检查数据是否存在,如下所示:
在您的情况下,
data
将为null
,并且null != ""
,因此 < code>if 正在通过。You could instead check if data exists, like this:
data
will benull
in your case, andnull != ""
, so theif
is passing.我也面临同样的问题。如果ajax没有返回数据,则数据块不会执行。但找到办法通过参考jQuery Ajax文档来解决:
http://api.jquery.com/jQuery .ajax/
在实现中我们使用了done的success方法,如果没有返回数据则不会执行。相反,将调用失败方法。参考
I also face the same issue. The data block will not execute of if data not returned from ajax. But find a way to solved by referring jQuery Ajax documents:
http://api.jquery.com/jQuery.ajax/
By the implementation we have used the sucess method of done and if data not returned it will not executed. Instead fail method will get called. Refer to
如果 data 为空,则检查
data !== ''
的条件将生成 falseIf data is empty then condition for check
data !== ''
it will generate false