【发布时间】: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