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?
-
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
5 Answers
Reset to default 8This 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) likeif (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
版权声明:本文标题:jquery - javascript if statement checking for truefalse - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504812a2382257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论