逻辑和运算符在 JavaScript 中无法正常工作
我正在检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试检查您的布尔值是否大于 0。
您不需要第二个
> 0
:实际上,这对操作没有任何影响,但仍然不需要。
该代码对我来说似乎工作得很好(稍微适合测试):
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
: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/
您正在从
hasWhiteSpace()
返回一个布尔值,因此您应该跳过> 0
在你的条件中。You're returning a boolean value out of
hasWhiteSpace()
, so you should skip the> 0
in your conditional.您确定是操作符
&&
失败了吗?如果您将代码重写为:
现在设置一个断点(或使用
alert
输出)来检查lblNotZero
和hasWhiteSpace
的值。每当您怀疑
&&
等语言基础知识无法正常工作时,请重新思考。这些都经过了非常好的测试。Are you sure that it is operator
&&
that fails?What if you rewrite your code to:
Now set a breakpoint (or output using
alert
) to check the values oflblNotZero
andhasWhiteSpace
.Whenever you suspect language fundamentels such as
&&
not working correctly, rethink. Those are extremely well tested.