javascript下一种表单元素获取方法存在的问题
作者:bea
一. 测试环境 浏览器: IE6+, FF 3.5.5, Opera 10, Chrome 4.0.249, Safari 4.0.3 二. 例子 代码如下: <form name="test-form" action="" method=""> <input type="checkbox" name="kk"> <form> <script type="text/javascript"> var oForm =
一. 测试环境
浏览器: IE6+, FF 3.5.5, Opera 10, Chrome 4.0.249, Safari 4.0.3
二. 例子
代码如下:
<form name="test-form" action="" method="">
<input type="checkbox" name="kk">
<form>
<script type="text/javascript">
var oForm = document.forms['test-form'],
eles = oForm.elements['kk'];
alert(eles.length); // undefined
alert(eles.nodeType); // 1
</script>
三. 解决方法(我想到的方法是改变获取方式, 基于YUI)
代码如下:
<script type="text/javascript">
var oForm = document.forms['test-form'],
eles = YAHOO.util.Dom.getElementsBy(function(el) {
return el.type === 'checkbox' && el.name === 'kk';
}, 'input', oForm);
alert(eles.length); // 1
</script>
document.formname.inputname
这个问题我之前已经遇到过一次了,但是这次又忘记了,依然又犯错了,所以我必须要记录一下。
看一下这个例子:
代码如下:
<form name="hehe">
<input type="checkbox" name="haha" />
</form>
<form name="hehe2">
<input type="checkbox" name="haha" />
<input type="checkbox" name="haha" />
</form>
<script type="text/javascript">
document.write(document.hehe.haha.length);
document.write('<br />');
document.write(document.hehe2.haha.length);
</script>
演示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form name="hehe">
<input type="checkbox" name="haha" />
</form>
<form name="hehe2">
<input type="checkbox" name="haha" />
<input type="checkbox" name="haha" />
</form>
<script type="text/javascript">
document.write(document.hehe.haha.length);
document.write('<br>');
document.write(document.hehe2.haha.length);
</script>
</body>
</html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]
在获取表单的input时,我一般都习惯这么做,但是这样做往往就出现问题了,因为很多时候我会去对比已选的checkbox和所有的 checkbox数量是否相等,但是只有一个的时候就会出错,因为它返回的就是checkbox本身,并没有length这个属性,所以我们需要先判断所取到的input是否有length属性以便进一步操作。
另外记录下location.reload(),它还有一个可选参数,true或者false,如果省略或者设置为false,它就会用HTTP头 If-Modified-Since来检测服务器上的文档是否已改变。如果文档已改变,reload()会再次下载该文档。如果文档未改变,则该方法将从缓存中装载文档。这与用户单击浏览器的刷新按钮的效果是完全一样的。如果设置为true,那么无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下载该文档。就是传说中的强制刷新。
有用 | 无用
浏览器: IE6+, FF 3.5.5, Opera 10, Chrome 4.0.249, Safari 4.0.3
二. 例子
代码如下:
<form name="test-form" action="" method="">
<input type="checkbox" name="kk">
<form>
<script type="text/javascript">
var oForm = document.forms['test-form'],
eles = oForm.elements['kk'];
alert(eles.length); // undefined
alert(eles.nodeType); // 1
</script>
三. 解决方法(我想到的方法是改变获取方式, 基于YUI)
代码如下:
<script type="text/javascript">
var oForm = document.forms['test-form'],
eles = YAHOO.util.Dom.getElementsBy(function(el) {
return el.type === 'checkbox' && el.name === 'kk';
}, 'input', oForm);
alert(eles.length); // 1
</script>
document.formname.inputname
这个问题我之前已经遇到过一次了,但是这次又忘记了,依然又犯错了,所以我必须要记录一下。
看一下这个例子:
代码如下:
<form name="hehe">
<input type="checkbox" name="haha" />
</form>
<form name="hehe2">
<input type="checkbox" name="haha" />
<input type="checkbox" name="haha" />
</form>
<script type="text/javascript">
document.write(document.hehe.haha.length);
document.write('<br />');
document.write(document.hehe2.haha.length);
</script>
演示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form name="hehe">
<input type="checkbox" name="haha" />
</form>
<form name="hehe2">
<input type="checkbox" name="haha" />
<input type="checkbox" name="haha" />
</form>
<script type="text/javascript">
document.write(document.hehe.haha.length);
document.write('<br>');
document.write(document.hehe2.haha.length);
</script>
</body>
</html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]
在获取表单的input时,我一般都习惯这么做,但是这样做往往就出现问题了,因为很多时候我会去对比已选的checkbox和所有的 checkbox数量是否相等,但是只有一个的时候就会出错,因为它返回的就是checkbox本身,并没有length这个属性,所以我们需要先判断所取到的input是否有length属性以便进一步操作。
另外记录下location.reload(),它还有一个可选参数,true或者false,如果省略或者设置为false,它就会用HTTP头 If-Modified-Since来检测服务器上的文档是否已改变。如果文档已改变,reload()会再次下载该文档。如果文档未改变,则该方法将从缓存中装载文档。这与用户单击浏览器的刷新按钮的效果是完全一样的。如果设置为true,那么无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下载该文档。就是传说中的强制刷新。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- Extjs 几个方法的讨论
- JavaScript 判断判断某个对象是Object还是一个Array
- 图像替换新技术 状态域方法
- javascript innerText和innerHtml应用
- js 右键菜单,支持不同对象不同菜单(兼容IE、Firefox)
- 使用JavaScript库还是自己写代码?
- js或css实现滚动广告的几种方案
- jquery中的sortable排序之后的保存状态的解决方法
- javascript面向对象的方式实现的弹出层效果代码
- js提示信息jtip封装代码,可以是图片或文章
- jquery 常用操作方法
- jquery 经典动画菜单效果代码
- 使用JQuery进行跨域请求
- JavaScript 使用简略语法创建对象的代码
- javascript 的Document属性和方法集合
- 起点页面传值js,有空研究学习下
- javascript 自动标记来自搜索结果页的关键字
- js 多层叠的TAB选项卡
- javascript 三种数组复制方法的性能对比