how we can covert a string to boolean?

Possible Duplicate:
How can I convert a string to boolean in JavaScript?

I have a select list with 2 options in it, yes or no , something like:

   <select size="1">
     <option value="true">yes</option>
     <option value="false">no</option>
   </select>

Now i want to use the selected value in the jquery-UI button disabled property , means :

  $("button").button({ disabled : $("select").val() });

Now my problem is that the value which we will get by $("select").val() is string and for

disabled property we need boolean. So i want to know that is there any method just like

pareInt or parseFloat by which we can convert a string to boolean ?


var value = ('true' === $("select").val() );

You can use the third one:

var num = +something; //toNumber
var str = something + ""; //toString
var bol = !!something; //toBoolean

That will turn 0, "", false, null, undefined, NaN to false , and everything else to true

But using my deduction powers, you want something like "false" -> false , for this you can use one of these:

var bol = something === "true"; //false for anything different than true
var bol = something !== "false"; //true for anything different than false

var myBoolean = (myString === 'true') ? true : false;
链接地址: http://www.djcxy.com/p/75078.html

上一篇: 方法返回True,它不是布尔类型

下一篇: 我们如何将一个字符串转换为布尔值?