javascript - Storing boolean values in hidden fields for form processing - Stack Overflow

admin2025-04-19  0

I have read a lot of good stuff on truthy and falsy (.html), and in particular the differences between '==' and '===' in JavaScript. The best explanations and resources mainly from this Stack Overflow Question:

Which equals operator (== vs ===) should be used in JavaScript parisons?

I am working on a form and need to store a boolean value into a hidden field with the following jQuery syntax:

$('[name="fieldName"]').val(true);

So I am inserting a boolean value, true or false, into the hidden field. During on screen processing, I grab this field value in a number of if-statements with the following code, and pare it as follows:

$('[name="fieldName"]').val() === "true"

Note how I am using the === operator to pare to the string value "true".

My question; if only Object in JavaScript is of type reference, then boolean must be a value type. I would therefore assume, as value parisons with the === operator pare not only contents but the type, that the above expression should return false? However in my code it returns true - can anyone explain why this is?

This code previously used == for the parison, but to avoid falsy risks I am implementing === wherever possible. Could it be something to do with jQuery converting the values for me?

I have read a lot of good stuff on truthy and falsy (http://adripofjavascript./blog/drips/truthy-and-falsy-values-in-javascript.html), and in particular the differences between '==' and '===' in JavaScript. The best explanations and resources mainly from this Stack Overflow Question:

Which equals operator (== vs ===) should be used in JavaScript parisons?

I am working on a form and need to store a boolean value into a hidden field with the following jQuery syntax:

$('[name="fieldName"]').val(true);

So I am inserting a boolean value, true or false, into the hidden field. During on screen processing, I grab this field value in a number of if-statements with the following code, and pare it as follows:

$('[name="fieldName"]').val() === "true"

Note how I am using the === operator to pare to the string value "true".

My question; if only Object in JavaScript is of type reference, then boolean must be a value type. I would therefore assume, as value parisons with the === operator pare not only contents but the type, that the above expression should return false? However in my code it returns true - can anyone explain why this is?

This code previously used == for the parison, but to avoid falsy risks I am implementing === wherever possible. Could it be something to do with jQuery converting the values for me?

Share Improve this question edited May 23, 2017 at 12:07 CommunityBot 11 silver badge asked Apr 27, 2016 at 15:39 coderwurstcoderwurst 1593 silver badges14 bronze badges 1
  • What is $j in the paring code? – Rehban Khatri Commented Apr 27, 2016 at 15:47
Add a ment  | 

2 Answers 2

Reset to default 3

Could it be something to do with jQuery converting the values for me?

HTML form elements always store the value as a string, regardless of what the actual content is. In your case it is a boolean, but even if it was say a number type, what you would see is "10.5" not 10.5.

If you'd really like to pare boolean to boolean, what I would remend is:

Boolean($j('[name="fieldName"]').val()) === true

Boolean() approach will not work because:

Boolean("true") === true;
Boolean("false") === true;

Seems that only way it is:

$j('[name="fieldName"]').val(true)
$j('[name="fieldName"]').val() === "true"
or
$j('[name="fieldName"]').val(1)
$j('[name="fieldName"]').val() === "1"
etc.
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745021682a280418.html

最新回复(0)