Jquery/ASP 中的未知未捕获类型错误

Unknown Uncaught Type Error in Jquery/ASP

我需要提交按钮来检查单选框的空状态,同时与 asp 连接以包含 asp 计数器变量。对我来说,一个大危险信号是这段代码在页面上显示为原始文本,就好像它从代码中完全删除了脚本标签一样。如果第4行代码被注释掉,就不会这样显示了

到目前为止,我已经构建了这段代码,但是当按下提交按钮时它没有触发。

<%
TmpString = ""
TmpString = TmpString & "<script type=" & chr(34) & "text/javascript" & chr(34) & ">"

TmpString = TmpString &"$(document).ready(function() {" 
    TmpString = "$(document).submit(function() {"
    TmpString = TmpString & chr(10) & chr(13)
        For z = 0 to TotalUnits -1
            TmpString = TmpString & "var genericValue" & z & "= document.getElementsByName(" & chr(34) & "checkbox" & z & chr(34) & ").value;"
            TmpString = TmpString & chr(10) & chr(13)
            TmpString = TmpString & "If genericValue" & z & " == Null {"
            TmpString = TmpString & chr(10) & chr(13)
                TmpString = TmpString & "alert(" & chr(34) & "Select Yes or No for checkbox" & chr(34) & ") };" 
                TmpString = TmpString & chr(10) & chr(13)
            TmpString = TmpString & "return; });"
            TmpString = TmpString & chr(10) & chr(13)
        Next
            TmpString = TmpString &"$(" & chr(34) & "button" & chr(34) & ").click(function() {"
            TmpString = TmpString & chr(10) & chr(13)
            TmpString = TmpString & "$(" & chr(34) & "formID" & chr(34) & ").submit() }); });"
            TmpString = TmpString & chr(10) & chr(13)
        TmpString = TmpString & "</script>"
Response.Write (TmpString)
%> 

'收音机HTML

                TmpString = TmpString & "<label id=" & chr(34) & "checklabel" & chr(34)
                TmpString = TmpString & " name=" & chr(34) & "checklabel" & chr(34)
                TmpString = TmpString & ">The Vehicle Requires Repair</label>"


                TmpString = TmpString & "<label id=" & chr(34) & "yesno" & chr(34) & ">"
                TmpString = TmpString & "<input type=" & chr(34) & "radio" & chr(34) & "name=" & chr(34) & "checkradio" & z & chr(34)
                TmpString = TmpString & "value=" & chr(34) & "1" & chr(34) & "id=" & chr(34) & "Radio1" & chr(34) & ">Yes</label>"
                TmpString = TmpString & "<label id=" & chr(34) & "yesno" & chr(34) & ">"
                TmpString = TmpString & "<input type=" & chr(34) & "radio" & chr(34) & "name=" & chr(34) & "checkradio" & z & chr(34)
                TmpString = TmpString & "value=" & chr(34) & "0" & chr(34) & "id=" & chr(34) & "Radio0" & chr(34) & ">No</label><br>"

这是我在浏览器控制台中得到的错误:

"Uncaught TypeError: Cannot read property 'addEventListener' of null"

我认为您在控制台中遇到了错误,因为您发送到浏览器的 javascript 根本无效。

您提供的代码(非常简单)糟糕,因此不容易看到代码将产生的 javascript 错误...

  • If genericValue1 == Null { 永远不会像 javascript 那样工作。
  • $("formID").submit(); 将不起作用,因为您需要在 ID
  • 之前添加 #

正如你所说的,这是经典ASP,它可以写成如下...

<script type="text/javascript">
  $(function() {
    $(document).submit(function() {
      <%For z = 0 to TotalUnits - 1%>
      var genericValue<%=z%> = document.getElementsByName("checkbox<%=z%>").value;
      if (genericValue<%=z%> == null) {
        alert("Select Yes or No for checkbox <%=z%>");
        return false;
      }
      <%Next%>
      $("button").click(function() {
        $("#formID").submit();
      });
    });
  });
</script>

我希望你能看到它更容易阅读和理解