Javascript 从javascript调用一个jsp页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3741290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
from javascript call a jsp page
提问by Mohan
I am new to stackoverflow i have doubt regarding calling jsp from javascript file. my file contain one html file with javascript(home.html) and one jsp file(login.jsp) In html(home.html) file i have 2 textbox and 2 buttons one for login and another for reset.when i click the login button i should call a js for textbox field validation (ie for if any one of the textbox is empty it shows "text fields should not be empty" alert msg to user)if both the textbox have value then it should call a jsp page(login.jsp).Thanks in advance
我是 stackoverflow 的新手,我对从 javascript 文件调用 jsp 有疑问。我的文件包含一个带有 javascript(home.html) 的 html 文件和一个 jsp 文件(login.jsp) 在 html(home.html) 文件中,我有 2 个文本框和 2 个按钮,一个用于登录,另一个用于重置。当我单击登录时按钮我应该调用一个 js 进行文本框字段验证(即,如果任何一个文本框为空,它会向用户显示“文本字段不应为空”警报消息)如果两个文本框都有值,那么它应该调用一个 jsp 页面( login.jsp)。提前致谢
采纳答案by Pramendra Gupta
Try using jquery
尝试使用jquery
$.post("login.jsp", { name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
});
回答by Delan Azabani
<form id="myform" action="login.jsp" method="post">
<input name="u" id="u"> Username<br>
<input name="p" id="p" type="password"> Password
</form>
<script>
document.getElementById('myform').addEventListener('submit', function(e) {
if (!document.getElementById('u').value || !document.getElementById('p').value)
e.preventDefault();
}, false);
</script>
回答by Mohan
<script language="JavaScript">
function val(){
var name=...
var pass=...
if(name==" "||pass==" ")
{
alert("fields should not be empty");
}
else{
var jspcall = "login.jsp?param1=value1¶m2=value2";
window.location.href = jspcall;
}
}
</script>