逻辑和运算符在 JavaScript 中无法正常工作

发布于 2024-12-18 08:58:29 字数 390 浏览 1 评论 0原文

我正在检查 documnet.ready 和 && 中的某些条件操作员不工作 代码片段 lbl 值为空。

$(function () {
 var lblValue = $("#lblRadioText").text();
    if (lblValue.length > 0 && hasWhiteSpace(lblValue) > 0) {
        $("#rdExportLastTime").css('display', 'inline');
    }
});

function hasWhiteSpace(text) {
    return text.indexOf(' ') >= 0;
}

你能告诉我出了什么问题吗?

i am checking certain condition in documnet.ready and && operator is not working
code snippet
lblvalue is empty.

$(function () {
 var lblValue = $("#lblRadioText").text();
    if (lblValue.length > 0 && hasWhiteSpace(lblValue) > 0) {
        $("#rdExportLastTime").css('display', 'inline');
    }
});

function hasWhiteSpace(text) {
    return text.indexOf(' ') >= 0;
}

can you tell me what is wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

苏大泽ㄣ 2024-12-25 08:58:29

您正在尝试检查您的布尔值是否大于 0。

您不需要第二个 > 0

if (lblValue.length > 0 && hasWhiteSpace(lblValue)) {
    $("#rdExportLastTime").css('display', 'inline');
}

实际上,这对操作没有任何影响,但仍然不需要。

该代码对我来说似乎工作得很好(稍微适合测试):
http://jsfiddle.net/infernalbadger/ZGAj5/1/

You are trying to check if your boolean value is great than 0.

You don't need the 2nd > 0:

if (lblValue.length > 0 && hasWhiteSpace(lblValue)) {
    $("#rdExportLastTime").css('display', 'inline');
}

Actually, that doesn't make any difference to the operation but it's still not needed.

The code seems to work fine to me (slightly adapted for testing):
http://jsfiddle.net/infernalbadger/ZGAj5/1/

静谧幽蓝 2024-12-25 08:58:29

您正在从 hasWhiteSpace() 返回一个布尔值,因此您应该跳过 > 0 在你的条件中。

You're returning a boolean value out of hasWhiteSpace(), so you should skip the > 0 in your conditional.

爱冒险 2024-12-25 08:58:29

您确定是操作符 && 失败了吗?

如果您将代码重写为:

var lblNotZero = lblValue.length > 0;
var hasWhiteSpace = hasWhiteSpace(lblValue) > 0;
if(lblNotZero && hasWhiteSpace)
{
  $("#rdExportLastTime").css('display', 'inline');
}

现在设置一个断点(或使用 alert 输出)来检查 lblNotZerohasWhiteSpace 的值。

每当您怀疑 && 等语言基础知识无法正常工作时,请重新思考。这些都经过了非常好的测试。

Are you sure that it is operator && that fails?

What if you rewrite your code to:

var lblNotZero = lblValue.length > 0;
var hasWhiteSpace = hasWhiteSpace(lblValue) > 0;
if(lblNotZero && hasWhiteSpace)
{
  $("#rdExportLastTime").css('display', 'inline');
}

Now set a breakpoint (or output using alert) to check the values of lblNotZero and hasWhiteSpace.

Whenever you suspect language fundamentels such as && not working correctly, rethink. Those are extremely well tested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文