表单域name和js函数重名引起的问题

无意中遇到这个问题,如下面的html代码:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. <script type="text/javascript">
  7. function add(){
  8.     alert("call add method.");
  9. }
  10. function input(){
  11.     alert("call input method");
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. <form>
  17.     <input type="button" name="add" value="添加" onclick="add()" />
  18.     <input type="text" name="input" value="输入" onclick="input()" />
  19. </form>
  20. </body>
  21. </html>

在IE中运行提示错误:对象不支持此属性或方法

在FF中运行提示错误:add is not a function 和 input is not a function

以前并没有遇到这样的错误,郁闷好久之后,一气之下把修改了add和input的方法名为add1和input1,结果能正常运行了。

于是联想到FF中的错误提示,猜测出错的原因是按钮的名称add和函数名add重复了,于是又尝试了如下代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. <script type="text/javascript">
  7. function add(){
  8.     alert("call add method.");
  9. }
  10. function input(){
  11.     alert("call input method");
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. <form>
  17.     <input type="button" name="input" value="添加" onclick="add()" />
  18.     <input type="text" name="add" value="输入" onclick="input()" />
  19. </form>
  20. </body>
  21. </html>

仍然会报出相同的错误,这就更验证了我的猜测。

再尝试下列代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. <script type="text/javascript">
  7. function add1(obj){
  8.     //alert("call add method.");
  9.     alert(obj.value);
  10. }
  11. function input1(){
  12.     alert("call input method");
  13. }
  14. </script>
  15. </head>
  16. <body>
  17. <form name="form1">
  18.     <input type="button" name="add" value="添加" onclick="add1(input)" />
  19.     <input type="text" name="input" value="输入" onclick="input1()" />
  20. </form>
  21. <form name="form2">
  22.     <input type="button" name="btn" value="Form2_按钮" onclick="alert(add.value);" />
  23. </form>
  24. </body>
  25. </html>

会发现 form1的按钮[添加]和onclick事件能正常执行, form2的按钮[Form2_按钮]onclick事件出错。

再尝试下面2段代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. <script type="text/javascript">
  7. function add1(obj){
  8.     //alert("call add method.");
  9.     alert(obj.value);
  10. }
  11. function input1(){
  12.     //alert("call input method");
  13.     alert(form2.btn.value);
  14. }
  15. </script>
  16. </head>
  17. <body>
  18. <form name="form1">
  19.     <input type="button" name="add" value="添加" onclick="add1(input)" />
  20.     <input type="text" name="input" value="输入" onclick="input1()" />
  21. </form>
  22. <form name="form2">
  23.     <input type="button" name="btn" value="Form2_按钮" onclick="alert(form1.add.value);" />
  24. </form>
  25. </body>
  26. </html>
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. <script type="text/javascript">
  7. function add1(obj){
  8.     //alert("call add method.");
  9.     alert(obj.value);
  10. }
  11. function input1(){
  12.     //alert("call input method");
  13.     alert(form2.btn.value);
  14. }
  15. function btn(){
  16.     alert("call btn method");
  17. }
  18. </script>
  19. </head>
  20. <body>
  21. <form name="form1">
  22.     <input type="button" name="add" value="添加" onclick="btn()" />
  23.     <input type="text" name="input" value="输入" onclick="input1()" />
  24. </form>
  25. <form name="form2">
  26.     <input type="button" name="btn" value="Form2_按钮" onclick="alert(form1.add.value);" />
  27. </form>
  28. </body>
  29. </html>

会发现事件执行的很好。

于是就想到了,

1.form是可以直接通过name来调用对应的form对象。

2.在同一form中,各个表单域又可以直接通过各自的name来调用各自的对象。

3.由于js中,函数也是对象,于是就引发了第一个代码段中的问题。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值