<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function check(form){
alert("[" + form.age.value + "]");// [ ]
    alert(typeof(form.age.value));//string
alert(form.age.value==" ");//IE ture, firefox/chrome> false
return false;
}
</script>
</head><body>
<form onsubmit="check(this);" method="post" action="#">
<select name="age">
<option>&nbsp;</option>
<option value="18">18</option>
</select>
<input type="submit" value="submit" />
</form>
</body>
</html>
用firebug看到此时option的值是" ",为何firefox和chrome alert(form.age.value==" ");等于false
,而IE为true。在firefox中怎么写才能变成true,也即此时value究竟是什么?

解决方案 »

  1.   

    form.age.selectedIndex ==0这样呢?
      

  2.   


    我在ie9下测得也是false,而且值为""
    火狐就不知道为什么了
    这样改下吧 
    function check(form){
        alert("[" + form.age.value + "]");// [ ]
        alert(typeof(form.age.value));//string
        alert(form.age.value.replace(/^\s+/,"")=="");//IE ture, firefox/chrome> false
        return false;
    }
      

  3.   

        <select name="age">
            <option value="">&nbsp;</option>
            <option value="18">18</option>
        </select>
      

  4.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function check(form){
        alert("[" + form.age.value + "]");// [ ]
        alert(typeof(form.age.value));//string
        alert(form.age.value==" ");//改成空字符串,不要加空格。
        return false;
    }
    </script>
    </head><body>
    <form onsubmit="check(this);" method="post" action="#">
        <select name="age">
            <option value="">&nbsp;</option>
            <option value="18">18</option>
        </select>
        <input type="submit" value="submit" />
    </form>
    </body>
    </html>
      

  5.   

    我知道解决办法,只是想知道为何会有如此差异,并且在firefox中究竟此时为何值
      

  6.   

    <form onsubmit="check(this);" method="post" action="#">表单 没有name 
    <form onsubmit="check(this);" method="post" action="#" name='myForm'>alert("[" + document.myForm.age.value + "]")