admin管理员组

文章数量:1290982

I wrote, what I thought, was a straight forward if statement in JS but it is running incorrectly.

function printLetter(LetterId) {
    var studentflag = $("#IsStudent").val();
    if (studentflag)
    {
           //do option 1        
    } else {

           //do option 2    
    }

}

Everytime it runs, the studentflag var value is correct, but regardless of whether it is true or false, it goes into option 1. I am pretty sure I have done true/false checks like this before in JS, but do I need to spell it out (studentflag == true) instead?

I wrote, what I thought, was a straight forward if statement in JS but it is running incorrectly.

function printLetter(LetterId) {
    var studentflag = $("#IsStudent").val();
    if (studentflag)
    {
           //do option 1        
    } else {

           //do option 2    
    }

}

Everytime it runs, the studentflag var value is correct, but regardless of whether it is true or false, it goes into option 1. I am pretty sure I have done true/false checks like this before in JS, but do I need to spell it out (studentflag == true) instead?

Share Improve this question edited Aug 25, 2014 at 20:32 falsarella 12.4k10 gold badges74 silver badges118 bronze badges asked Aug 25, 2014 at 20:23 BattlFrogBattlFrog 3,4078 gold badges60 silver badges88 bronze badges 4
  • 2 It's hard to tell without knowing to what #IsStudent refers. – cdhowie Commented Aug 25, 2014 at 20:24
  • 1 At first glance your code looks fine, have you tried doing a console.log on studentFlag before your if statement? What was the result? – Undefined Commented Aug 25, 2014 at 20:25
  • @cdhowie isn't cause val() returns not boolean? – Alex Char Commented Aug 25, 2014 at 20:25
  • 1 @Alek .val() will always return you a string, just sometimes it could be an empty string. Empty strings are false and would follow the logic laid out in the question – Undefined Commented Aug 25, 2014 at 20:26
Add a ment  | 

5 Answers 5

Reset to default 8

This is known as truthy and falsy Values

The following values are always falsy:

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (a special Number value meaning Not-a-Number!)

All other values are truthy:

  • including "0" (zero in quotes),
  • "false" (false in quotes) like if (studentflag) //being studentflag "false",
  • empty functions,
  • empty arrays, and
  • empty objects.

If #StudentFlag is either "true" or "false", then if(studentFlag) will always follow the true route because both are non-empty strings (truthy). You need to do something along these lines:

var studentflag = $("#IsStudent").val();
if (studentflag === "true") {
       //do option 1        
} else {
       //do option 2    
}

.val () doesn't return a boolean.

Try this instead;

function printLetter(LetterId) {
    var studentflag = $("#IsStudent").is (':checked');
    if (studentflag)
    {
           //do option 1        
    } else {

           //do option 2    
    }

}

This is assuming #IsStudent is a checkbox. If it's not, try this (assuming the value is true (as a string, not a boolean));

function printLetter(LetterId) {
    var studentflag = ($("#IsStudent").val () == 'true')
    if (studentflag)
    {
           //do option 1        
    } else {

           //do option 2    
    }

}

IMO there should be more context in the question. If submitted solution works for OP that is great, but for others using this as a resource, the accepted solution might not work in all cases.

The value retrieved from an element via JS actually depends on the input itself and its HTML structure. Here's a demo explaining the difference between using .val(), .attr('val'), and .is(':checked') with checkboxes and radios. All of those variants can pull different values from an element depending on its HTML structure and current UI state.

Fiddle : http://jsfiddle/h6csLaun/2/

var studentflag = $("#IsStudent").val();//This is a string .. not a boolean
if (studentflag === "true")  //therefore this has to be string parison

Or you can make studentflag boolean as follows:

var studentflag = $("#IsStudent").val() === "true";
if (studentflag) { ....

本文标签: jqueryjavascript if statement checking for truefalseStack Overflow