jQuery 检查返回的数据对象是否为空

发布于 2024-08-27 21:51:02 字数 376 浏览 4 评论 0原文

$("#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦过后 2024-09-03 21:51:02

您可以检查数据是否存在,如下所示:

if(data && data !="") alert(data);

在您的情况下,data 将为 null,并且 null != "",因此 < code>if 正在通过。

You could instead check if data exists, like this:

if(data && data !="") alert(data);

data will be null in your case, and null != "", so the if is passing.

征棹 2024-09-03 21:51:02

我也面临同样的问题。如果ajax没有返回数据,则数据块不会执行。但找到办法通过参考jQuery Ajax文档来解决:

http://api.jquery.com/jQuery .ajax/

在实现中我们使用了done的success方法,如果没有返回数据则不会执行。相反,将调用失败方法。参考

fail(function())

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

fail(function())
离笑几人歌 2024-09-03 21:51:02

如果 data 为空,则检查 data !== '' 的条件将生成 false

if(data !== 'undefined' && data !== ''){
console.log('data is not empty')
} else {
console.log('data is empty')
}

If data is empty then condition for check data !== '' it will generate false

if(data !== 'undefined' && data !== ''){
console.log('data is not empty')
} else {
console.log('data is empty')
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文