option标签值获取在ie和firefox标准浏览器之间的差别???
JScript code
<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> </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究竟是什么?
------解决方案--------------------
我在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;
}
------解决方案--------------------
<form onsubmit="check(this);" method="post" action="#">
表单 没有name
<form onsubmit="check(this);" method="post" action="#" name='myForm'>
alert("[" + document.myForm.age.value + "]")