输入值以JavaScript显示
我想将输入值传递给java脚本,它来自会话。它没有发生。请帮忙输入值以JavaScript显示,javascript,java,html,jsp,Javascript,Java,Html,Jsp,我想将输入值传递给java脚本,它来自会话。它没有发生。请帮忙 函数applicationlogin(){ var userType=$('#userType').val(); 如果(用户类型==='admin'){ window.location.href=“a.jsp”; }else if(用户类型==='monitoringManger'){ window.location.href=“b.jsp”; }else if(userType=='ma'){ window.location.h
函数applicationlogin(){
var userType=$('#userType').val();
如果(用户类型==='admin'){
window.location.href=“a.jsp”;
}else if(用户类型==='monitoringManger'){
window.location.href=“b.jsp”;
}else if(userType=='ma'){
window.location.href=“c.jsp”;
}else if(userType=='superAdmin'){
window.location.href=“d.jsp”;
}else if(userType=='admin1'){
window.location.href=“e.jsp”;
}else if(userType=='la'){
window.location.href=“f.jsp”;
}else if(userType==='ia'){
window.location.href=“g.jsp”;
}else if(userType=='da'){
window.location.href=“h.jsp”;
}
}
重定向。。。
会话值在输入字段中正确输入。但不是在JS中。您知道您也在使用
jQuery
。。。这将要求您链接到jQuery库…因为java脚本在呈现页面之前加载。因此您将得到未定义的。在末尾添加else{console.log(userType);}
并将代码更改为使用$(document.ready(function()){
而不是body onload
这一点一开始就毫无意义。与其从服务器向客户端发送一个值,而只是让客户端立即基于此请求一个不同的URL,不如首先使用服务器端重定向。
<script type="text/javascript">
function applicantLogin() {
var userType = $('#userType').val();
if (userType === 'admin') {
window.location.href = "a.jsp";
}else if (userType === 'monitoringManger') {
window.location.href = "b.jsp";
}else if (userType === 'ma') {
window.location.href = "c.jsp";
}else if (userType === 'superAdmin') {
window.location.href = "d.jsp";
}else if (userType === 'admin1') {
window.location.href = "e.jsp";
}else if (userType === 'la') {
window.location.href = "f.jsp";
}else if (userType === 'ia') {
window.location.href = "g.jsp";
}else if (userType === 'da') {
window.location.href = "h.jsp";
}
}
</script>
<body onload="applicantLogin()">
<input type="hidden" id="userType" value="<%=session.getAttribute("userType").toString()%>"/>
<h1>Redirecting ...</h1>
</body>