【问题标题】:IE error 'var' is undefinedIE 错误 'var' 未定义
【发布时间】:2013-06-06 04:58:02
【问题描述】:

我的应用程序中有一个自动完成输入字段。当带有此自动完成输入字段的 jsp 打开时,我在<body onload="getNames(names)"> 行中收到错误'names' is undefined。这仅在 IE8、7、9 中发生。在 Chrome 和 Firefox 中,一切都很好。在控制器中,我获取用户的姓名和姓氏并将它们写入一个用$ 分隔的字符串,然后我将该字符串allUsersRoleUserAC 发送到jsp。方法 getNames(names) 拆分该字符串并将它们放入 var tags

这里是jsp代码:

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Admin page</title>

<link rel="stylesheet" type="text/css" href="<%=response.encodeURL(request.getContextPath() + "/css/jquery-ui.css") %>" />
<link rel="Stylesheet" type="text/css" href="<%=response.encodeURL(request.getContextPath() + "/css/DOMAlert.css") %>" media="screen" />
<script type="text/javascript" src="<%=response.encodeURL(request.getContextPath() + "/JavaScript/DOMAlert.js") %>"></script>
<script type="text/javascript" src="<%=response.encodeURL(request.getContextPath() + "/JavaScript/jquery-1.8.2.js") %>"></script>
<script type="text/javascript" src="<%=response.encodeURL(request.getContextPath() + "/JavaScript/jquery-ui.js") %>"></script>
<script type="text/javascript">

var tags = new Array();
function getNames(names){
    tags = names.value.split('$');
}

</script>

</head>
<body onload="getNames(names)" >

    <form action="<%=response.encodeURL(request.getContextPath() + "/admin/deleteUser.html") %>" method="post">
        <input id="names" type="hidden" value="${ requestScope['allUsersRoleUserAC'] }" />
        <input name="korisnik" class="userSelectDeleteUser" value="ime prezime" onclick="this.value=''" id="autocomplete" />
        <input class="izbrisiKorisnikaButtonPosition" id="buttonRightPart" type="button" value="Izbriši" />
    </form>

    <script type="text/javascript">
        $( "#autocomplete" ).autocomplete({
            source: function( request, response ) {
                var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
                response( $.grep( tags, function( item ){
                    return matcher.test( item );
                }) );
            }
        });
    </script>

</body>
</html>

【问题讨论】:

  • 与 Java/JSP 无关。了解如何使用 JavaScript/HTML 来实现,并且该逻辑在 JSP 中应该易于使用。
  • 您期望names 是什么?它没有在任何地方定义。
  • 不确定我是否理解你。我对此使用 javascript,它在 Firefox 中运行良好,但在 IE 中它不起作用。你知道我是否可以做一些事情来纠正这个问题以在 IE 中工作或一些解决方法?
  • 其实这本来是一个IE独有的东西。不敢相信它不再起作用了……IE/Chrome: are DOM tree elements global variables here?
  • names 是 id="names" 的输入字段

标签: javascript html internet-explorer autocomplete


【解决方案1】:
onload="getNames(names)"

names 是什么世界?您需要使用 getElementById 来引用元素。您不应该直接通过 id 引用元素。

onload="getNames(document.getElementById('names'));"

【讨论】:

    【解决方案2】:

    names 实际上是未定义的。其他浏览器只会传递未定义的值,而 IE 不会让你这样做。

    如果你想获得names 元素,那么你可以使用

    document.getElementById('names')
    

    我建议将其放在 onLoad 函数中,而不是将其作为参数传递,以将逻辑与标记分开。

    【讨论】:

      【解决方案3】:

      onload="getNames($("#names").val())"

      或非 jQuery

      onload="getNames(document.getElementById('names').value)"

      【讨论】:

      • 呃,这个页面肯定没有加载jQuery。
      • 哦,是的...我看到了`$("#autocomplete")`并且很兴奋
      • 我使用 jQuery,但没有粘贴所有内容以简化
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 2011-07-02
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多