问题:
Can I convert a string representing a boolean value (eg, 'true', 'false') into a intrinsic type in JavaScript? 我可以将表示布尔值(例如“ true”,“ false”)的字符串转换为JavaScript中的固有类型吗?
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. 但是,一旦将此值放入隐藏的输入字段中,它将成为一个字符串。
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. 一旦将字段转换为字符串,我可以找到确定该字段的布尔值的唯一方法就是依赖其字符串表示形式的文字值。
var myValue = document.myForm.IS_TRUE.value;
var isTrueSet = myValue == 'true';
Is there a better way to accomplish this? 有没有更好的方法可以做到这一点?
解决方案:
参考一: https://stackoom.com/question/16fV/如何在JavaScript中将字符串转换为布尔值参考二: https://oldbug.net/q/16fV/How-can-I-convert-a-string-to-boolean-in-JavaScript
来源:oschina
链接:https://my.oschina.net/u/4432649/blog/4463025