admin管理员组

文章数量:1026925

jsp page for submit button onclick function in javascript. when i click button first time not redirected to CallService.jsp. I click the button two to three times after that only that page will be redirected.

Pls give me the solution or reason

My Code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<script type="text/javascript">

function sample()
{

    var uname = document.getElementById("uname").value;
    //var pwd = document.getElementById("pwd").value;

    window.location.href("CallService.jsp?username="+uname);
}


</script>


<body>

<h3>User Login</h3> <br>

<form name="userlogin" method="post">

User Name : <input type="text" name="uname" id="uname"> <br>

Password : <input type="text" name="pwd" id="pwd"> <br>

<input type="submit" value="Submit" onclick="sample(this);" > 
<input type="reset" value="Cancel"> 

<div id="name"></div>
<div id="email"></div>

</form>

</body>
</html>

CallService.jsp

<%


  String uname = request.getParameter("username");
  //String pwd = request.getParameter("password");


  out.println(uname);

%>

jsp page for submit button onclick function in javascript. when i click button first time not redirected to CallService.jsp. I click the button two to three times after that only that page will be redirected.

Pls give me the solution or reason

My Code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<script type="text/javascript">

function sample()
{

    var uname = document.getElementById("uname").value;
    //var pwd = document.getElementById("pwd").value;

    window.location.href("CallService.jsp?username="+uname);
}


</script>


<body>

<h3>User Login</h3> <br>

<form name="userlogin" method="post">

User Name : <input type="text" name="uname" id="uname"> <br>

Password : <input type="text" name="pwd" id="pwd"> <br>

<input type="submit" value="Submit" onclick="sample(this);" > 
<input type="reset" value="Cancel"> 

<div id="name"></div>
<div id="email"></div>

</form>

</body>
</html>

CallService.jsp

<%


  String uname = request.getParameter("username");
  //String pwd = request.getParameter("password");


  out.println(uname);

%>
Share Improve this question asked Oct 24, 2013 at 8:01 Jagathesewaren KuppurajJagathesewaren Kuppuraj 911 gold badge3 silver badges11 bronze badges 1
  • 4 You are submitting form and redirecting to another form same time. It's not right – karaxuna Commented Oct 24, 2013 at 8:04
Add a ment  | 

6 Answers 6

Reset to default 2

Your button type is

<input type="submit" value="Submit" onclick="sample(this);" > 

replace it with:

<input type="button" value="Submit" onclick="sample(this);" > 

or

Call your function when submitting form like this:

<form method="post" name="fromname" onsubmit="yourfunction()">

Try this:

window.location.href = "CallService.jsp?username="+uname;

You are submitting form and redirecting to another form same time. It's not right

<input type="submit" value="Submit" onclick="sample(this);" > 

You cannot submit and redirect this.

In your servlet / jsp where the form is submitted, redirect the page.

please try:

function sample()
{

    var uname = document.getElementById("uname").value;
    //var pwd = document.getElementById("pwd").value;

    window.location.href("CallService.jsp?username="+uname);
    //FUNCTION MUST RETURN FALSE!!
    return false;
}

<input type="submit" value="Submit" onclick="return sample(this);" > 

this way js function will need to be evaluted before making postback

Why dont you try redirecting from you java class, like this

actionResponse.sendRedirect("CallService.jsp");

Since you are fetching the data using javascript here :

var uname = document.getElementById("uname").value;

theres no need to use a 'submit' button. Try to change

<input type="submit" value="Submit" onclick="sample(this);" > 

into

<input type="button" value="Submit" onclick="sample(this);" > 

jsp page for submit button onclick function in javascript. when i click button first time not redirected to CallService.jsp. I click the button two to three times after that only that page will be redirected.

Pls give me the solution or reason

My Code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<script type="text/javascript">

function sample()
{

    var uname = document.getElementById("uname").value;
    //var pwd = document.getElementById("pwd").value;

    window.location.href("CallService.jsp?username="+uname);
}


</script>


<body>

<h3>User Login</h3> <br>

<form name="userlogin" method="post">

User Name : <input type="text" name="uname" id="uname"> <br>

Password : <input type="text" name="pwd" id="pwd"> <br>

<input type="submit" value="Submit" onclick="sample(this);" > 
<input type="reset" value="Cancel"> 

<div id="name"></div>
<div id="email"></div>

</form>

</body>
</html>

CallService.jsp

<%


  String uname = request.getParameter("username");
  //String pwd = request.getParameter("password");


  out.println(uname);

%>

jsp page for submit button onclick function in javascript. when i click button first time not redirected to CallService.jsp. I click the button two to three times after that only that page will be redirected.

Pls give me the solution or reason

My Code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<script type="text/javascript">

function sample()
{

    var uname = document.getElementById("uname").value;
    //var pwd = document.getElementById("pwd").value;

    window.location.href("CallService.jsp?username="+uname);
}


</script>


<body>

<h3>User Login</h3> <br>

<form name="userlogin" method="post">

User Name : <input type="text" name="uname" id="uname"> <br>

Password : <input type="text" name="pwd" id="pwd"> <br>

<input type="submit" value="Submit" onclick="sample(this);" > 
<input type="reset" value="Cancel"> 

<div id="name"></div>
<div id="email"></div>

</form>

</body>
</html>

CallService.jsp

<%


  String uname = request.getParameter("username");
  //String pwd = request.getParameter("password");


  out.println(uname);

%>
Share Improve this question asked Oct 24, 2013 at 8:01 Jagathesewaren KuppurajJagathesewaren Kuppuraj 911 gold badge3 silver badges11 bronze badges 1
  • 4 You are submitting form and redirecting to another form same time. It's not right – karaxuna Commented Oct 24, 2013 at 8:04
Add a ment  | 

6 Answers 6

Reset to default 2

Your button type is

<input type="submit" value="Submit" onclick="sample(this);" > 

replace it with:

<input type="button" value="Submit" onclick="sample(this);" > 

or

Call your function when submitting form like this:

<form method="post" name="fromname" onsubmit="yourfunction()">

Try this:

window.location.href = "CallService.jsp?username="+uname;

You are submitting form and redirecting to another form same time. It's not right

<input type="submit" value="Submit" onclick="sample(this);" > 

You cannot submit and redirect this.

In your servlet / jsp where the form is submitted, redirect the page.

please try:

function sample()
{

    var uname = document.getElementById("uname").value;
    //var pwd = document.getElementById("pwd").value;

    window.location.href("CallService.jsp?username="+uname);
    //FUNCTION MUST RETURN FALSE!!
    return false;
}

<input type="submit" value="Submit" onclick="return sample(this);" > 

this way js function will need to be evaluted before making postback

Why dont you try redirecting from you java class, like this

actionResponse.sendRedirect("CallService.jsp");

Since you are fetching the data using javascript here :

var uname = document.getElementById("uname").value;

theres no need to use a 'submit' button. Try to change

<input type="submit" value="Submit" onclick="sample(this);" > 

into

<input type="button" value="Submit" onclick="sample(this);" > 

本文标签: javawhat is the reason for Windowlocationhref not working sometimesStack Overflow