点击链接时,JavaScript代码不会执行

时间:2015-07-17 00:05:54

标签: javascript html

下面的代码应该允许我单击文本链接并启动我的javascript函数。但是,当我点击链接时,我没有反应。 我使用本教程来帮助我http://www.thesitewizard.com/archive/textsubmit.shtml

<html><body>
 <head>  
<script language="JavaScript" type="text/javascript">
            function getDSH(selectedField) 
            {
                document.Results.dshToPrint.value = selectedField;
                document.Results.getElementByID(selectedField).disabled = false;
                document.Results.submit();
            }
        </script>
    </head>
    <font size = '6' color = 'red'>Loop 1 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('1-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 2 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('2-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
<p><a href = "javascript:getDSH('2-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 3 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('3-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
<p><a href = "javascript:getDSH('3-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 4 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('4-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
<p><a href = "javascript:getDSH('4-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 5 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('5-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 6 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('6-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 7 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('7-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
<p><a href = "javascript:getDSH('7-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 8 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('8-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 9 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('9-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
<p><a href = "javascript:getDSH('9-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 10 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('10-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
<p><a href = "javascript:getDSH('10-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 11 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('11-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 12 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('12-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
<p><a href = "javascript:getDSH('12-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<p><a href = "javascript:getDSH('12-3');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 25GBase --allow-mix-speeds --ppm=2000000 </a></p> 
<font size = '6' color = 'red'>Loop 13 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('13-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
<p><a href = "javascript:getDSH('13-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
<p><a href = "javascript:getDSH('13-3');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 25GBase --allow-mix-speeds --ppm=2000000 </a></p> 
<form name = 'Results' method = 'post' action = '/cgi-bin/developer/dsh-generator-d.py'> 
<input type = 'hidden' name = '13-3' value = "[&quot;2015-07-03 14:46:00]" disabled> 
<input type = 'hidden' name = 'dshToPrint' > 
</form> 
</body></html>

是语法错误吗?

编辑:当我点击链接时,出现此错误: 未捕获的TypeError:document.Results.getElementByID不是函数

2 个答案:

答案 0 :(得分:2)

首先,尝试使用浏览器中的开发人员控制台(可能显示为&#34; Ctrl + Shift + I&#34;)。

对于这个问题:Javascript区分大小写,因此您的&#34; getElementByID()&#34;不上班;使用&#34; getElementById()&#34;代替。此外,它在调用document。[form] .getElementById()时不会工作,你应该直接调用它:document.getElementById(id)。不过,您的HTML中没有设置ID,因此点击次数将始终返回&#39; null&#39;对于函数中的此参数。

我建议您首先尝试一些更简单的示例,然后再回到这个特定的示例!

答案 1 :(得分:0)

script标记移至form的末尾。 JavaScript来自HTML

<html>
<body>
   <font size = '6' color = 'red'>Loop 1 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('1-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 2 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('2-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
   <p><a href = "javascript:getDSH('2-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 3 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('3-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
   <p><a href = "javascript:getDSH('3-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 4 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('4-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
   <p><a href = "javascript:getDSH('4-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 5 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('5-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 6 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('6-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 7 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('7-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
   <p><a href = "javascript:getDSH('7-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 8 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('8-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 9 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('9-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
   <p><a href = "javascript:getDSH('9-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 10 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('10-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
   <p><a href = "javascript:getDSH('10-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 11 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('11-1');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
   <font size = '6' color = 'red'>Loop 12 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('12-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
  <p><a href = "javascript:getDSH('12-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
  <p><a href = "javascript:getDSH('12-3');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 25GBase --allow-mix-speeds --ppm=2000000 </a></p> 
  <font size = '6' color = 'red'>Loop 13 --------------------------------------------------------------------------------</font><p><a href = "javascript:getDSH('13-1');"> ##! TEST FAIL: /switchCard/testVoltageMargining </a></p> 
  <p><a href = "javascript:getDSH('13-2');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 100GBase-4 --allow-mix-speeds --ppm=2000000 </a></p> 
  <p><a href = "javascript:getDSH('13-3');"> ##! TEST FAIL: /switchCard/testTraffic -l None -s 9208 -d 60 -r 25GBase --allow-mix-speeds --ppm=2000000 </a></p> 
  <form name = 'Results' method = 'post' action = '/cgi-bin/developer/dsh-generator-d.py'> 
  <input type = 'hidden' name = '13-3' value = "[&quot;2015-07-03 14:46:00]" disabled> 
  <input type = 'hidden' name = 'dshToPrint' > 
   </form>
<script language="JavaScript" type="text/javascript">
    function getDSH(selectedField) 
                {
                    document.Results.dshToPrint.value = selectedField;
                    document.Results.getElementByID(selectedField).disabled = false;
                    document.Results.submit();
                }
            </script>
    </body>
</html>