如何在JavaScript中将字符串转换为布尔值? - How can I convert a string to boolean in JavaScript?

问题:

Can I convert a string representing a boolean value (eg, 'true', 'false') into a intrinsic type in JavaScript? 我能够将表示布尔值(例如“ true”,“ false”)的字符串转换为JavaScript中的固有类型吗? this

I have a hidden form in HTML that is updated based upon a user's selection within a list. 我有一个隐藏的HTML表单,该表单会根据用户在列表中的选择进行更新。 This form contains some fields which represent boolean values and are dynamically populated with an intrinsic boolean value. 此表单包含一些表示布尔值的字段,并使用内部布尔值动态填充。 However, once this value is placed into the hidden input field it becomes a string. 可是,一旦将此值放入隐藏的输入字段中,它将成为一个字符串。 spa

The only way I could find to determine the field's boolean value, once it was converted into a string, was to depend upon the literal value of its string representation. 一旦将字段转换为字符串,我能够找到肯定该字段的布尔值的惟一方法就是依赖其字符串表示形式的文字值。 .net

var myValue = document.myForm.IS_TRUE.value;
var isTrueSet = myValue == 'true';

Is there a better way to accomplish this? 有没有更好的方法能够作到这一点? code


解决方案:

参考一: https://stackoom.com/question/16fV/如何在JavaScript中将字符串转换为布尔值
参考二: https://oldbug.net/q/16fV/How-can-I-convert-a-string-to-boolean-in-JavaScript
相关文章
相关标签/搜索