I have an asp.net webform with a textbox. The value of the textbox is "False" and has been verified by viewing the page source in the browser.
Despite being set to false the following code results in beginDateReqd being set to false and consequently, DateParms being displayed when it shouldn't be.
var beginDateReqd = Boolean($('.HiddenBeginDateTimeRequired').val());
if (beginDateReqd) {
$('.DateParms').show();
}
What am I doing wrong? Thanks!
-
Please check this existing question for more details. stackoverflow.com/questions/263965/…Saurabh Sharma– Saurabh Sharma2011年11月15日 19:38:06 +00:00Commented Nov 15, 2011 at 19:38
5 Answers 5
Safer would be first to convert value "toLowerCase" and then compare with "true" value:
var beginDateReqd = ($('.HiddenBeginDateTimeRequired').val().toLowerCase() == "true");
if (beginDateReqd) {
$('.DateParms').show();
}
Comments
Why not just use a comparison operator?
var beginDateReqd = ($('.HiddenBeginDateTimeRequired').val() == "True");
if (beginDateReqd) {
$('.DateParms').show();
}
Comments
var beginDateReqd = parseBoolean ($('.HiddenBeginDateTimeRequired').val());
if ( beginDateReqd ) {
$('.DateParms').show();
}
function parseBoolean(str) {
return /^true$/i.test(str);
}
3 Comments
isTrue() or throw an error if the value is not either true or false.foo == "True"?mystring.toLowerCase() == somestring.The Boolean object does not parse string values for truthiness. You should be using a comparison operator or a regex test. See http://www.w3schools.com/js/js_obj_boolean.asp
Comments
Use comparison after making sure of the case
var beginDateReqd = ($('.HiddenBeginDateTimeRequired').val().toLowerCase() == "true");