jquery - logical and operator not working properly in javascript - Stack Overflow

i am checking certain condition in documnet.ready and && operator is not workingcode snippet

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.

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.

Share Improve this question asked Nov 25, 2011 at 10:29 ankurankur 4,74315 gold badges67 silver badges104 bronze badges 2
  • 1 try if ((lblValue.length > 0) && (hasWhiteSpace(lblValue) > 0)) { so that both statements are enclosed within their own parentheses. – martincarlin87 Commented Nov 25, 2011 at 10:31
  • try this ...if (lblValue.length > 0 && (hasWhiteSpace(lblValue) > 0)) { – balaphp Commented Nov 25, 2011 at 10:32
Add a ment  | 

3 Answers 3

Reset to default 4

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/infernalbadger/ZGAj5/1/

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745132343a4613032.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信