<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究竟是什么?
<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究竟是什么?
解决方案 »
- 看了“内容耦合”定义有点儿一头雾水,谁能找个javascript例子解释一下
- 一道百思不解的难题
- 100分求一个不使用<script>导入外部JS的方法
- 100分求段js
- 用for循环生成多个checkbox但是name都一样,这样如何去实现选择一个复选框就显示一个testarea取消一个复选框就不显示刚才的testarea
- 用JS如何判断页面是否加了W3C声明
- 有关复数form传递数据的问题
- open打开问题
- 请问怎由三个表组成一颗DTree
- 关于在html中使用xml数据岛的性能问题(绝对有分!)
- JavaScript有没有专门的开发工具或者做到MyEclipse里的插件
- php一段代码不懂上来问下???
我在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;
}
<option value=""> </option>
<option value="18">18</option>
</select>
<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=""> </option>
<option value="18">18</option>
</select>
<input type="submit" value="submit" />
</form>
</body>
</html>
<form onsubmit="check(this);" method="post" action="#" name='myForm'>alert("[" + document.myForm.age.value + "]")