Java jsp 中的 href 标签并通过单击 href 标签传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23914394/
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
href tags in jsp's and passing data by clicking on href tag
提问by priyatham manne
this is my program
这是我的程序
<form method="post">
Movie Name :<input type="text" name="movie"/><br>
Hero:<input type="text" name="hero"><br>
Director:<input type="text" name="dir"><br>
<a href="insert.jsp">Insert</a>
<a href="update.jsp">update</a>
<a href="delete.jsp">Delete</a>
when i click on the any of the href link the values of above text boxes should also to be carried to that particular jsp page i used getparameter method but i am not getting what i entered but they are taking null values
当我点击任何一个 href 链接时,上述文本框的值也应该被带到那个特定的 jsp 页面,我使用了 getparameter 方法,但我没有得到我输入的内容,但他们采用的是空值
回答by hbCyber
This is because your href elements are simply redirecting to these other pages -- the form's data is scrapped.
这是因为您的 href 元素只是重定向到这些其他页面——表单的数据被废弃了。
What you want to do is allow the form to be submitted -- you will receive the values in your Servlet's doPost() implementation, and in that method, you should then redirect to the page by, perhaps, adding the values in the URL, for instance:
您想要做的是允许提交表单——您将收到 Servlet 的 doPost() 实现中的值,然后在该方法中,您应该重定向到页面,也许是通过在 URL 中添加值,例如:
insert.jsp?movie=jaws&hero=hercules&director=spielberg
The values will then be available to that page.
然后这些值将可用于该页面。
EDIT: based on my latest comment, to do this without POST/servlets, your code would look like that with jQuery enabled:
编辑:根据我的最新评论,要在没有 POST/servlets 的情况下执行此操作,您的代码在启用 jQuery 的情况下看起来像这样:
<form method="get" id="myForm">
Movie Name :<input type="text" name="movie"/><br>
Hero:<input type="text" name="hero"><br>
Director:<input type="text" name="dir"><br>
<a href="insert.jsp">Insert</a>
<a href="update.jsp">update</a>
<a href="delete.jsp">Delete</a>
</form>
<script>
$('#myForm A').click(function(e)
{
e.preventDefault(); // prevent the link from actually redirecting
var destination = $(this).attr('href');
$('#myForm').attr('action', destination);
$('#myForm').submit();
});
</script>