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

最佳答案

onload="getNames(names)"

世界是什么?您需要使用getElementById来引用元素。不应直接通过id引用元素。
onload="getNames(document.getElementById('names'));"

关于javascript - IE错误“var”未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17025544/

10-13 00:46