如何在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
相關文章
相關標籤/搜索