javascript 使用 onsubmit 或 action 重定向到另一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24300906/
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
redirect to another page using onsubmit or action?
提问by cooljohny
I am trying to direct to another page on particular event occurrence ...This is my code..but it does not direct to another page but the JavaScript code works...
我试图在特定事件发生时定向到另一个页面……这是我的代码……但它没有定向到另一个页面,但 JavaScript 代码有效……
<form class="list-group-item" method="get" onsubmit="action='Search.jsp'; myFunction('name');return false;" >
<input type="hidden" name="item" value="<%=1%>">
<i class="fa fa-arrow-circle-right fa-fw "></i> <span onsubmit="action='Search.jsp'" onclick="action='Search.jsp';document.getElementById('div1').style.display = 'block';setValue('Subject')" style="margin-left:1%">Subject </span>
<span class=" text-muted small" onclick="document.getElementById('div1').style.display = 'block';setValue('Subject And')" style="margin-left:40%"><em> And <i class="fa fa-angle-down "></i></em>
</span>
</form>
First I tried action="search.jsp" outside onsubmit but that didn't work..I am new to all this and I don't know what to do?
首先,我在 onsubmit 之外尝试了 action="search.jsp" 但这不起作用..我对这一切都很陌生,我不知道该怎么办?
回答by Jesuraja
Add an button inside <form>tag
在<form>标签内添加按钮
<input type="submit" value="Submit">
Add actionattribute in form tag
在表单标签中添加动作属性
action="Search.jsp"
Whenever you click Submitbutton, the page will be redirected to Search.jsp
每当您单击Submit按钮时,页面将被重定向到Search.jsp
回答by Parag Tyagi -morpheus-
1) If you simply want to get it redirected to Search.jspafter form submission, remove all crap JS code from your form and add a simple actionattribute in form tag.
2) Action attributebehaves as redirect if you submit the form.
3) Writing JS code within HTML Tags is a bad convention. Always write JS code within <script>..</script>blocks or a different JS fileand then include it.
1)如果您只是想Search.jsp在表单提交后将其重定向到,请从表单中删除所有垃圾 JS 代码并action在form tag.
2)Action attribute如果您提交表单,则表现为重定向。
3) 在 HTML 标签中编写 JS 代码是一个糟糕的约定。始终在<script>..</script>块内或不同的块内编写 JS 代码JS file,然后将其包含在内。
<form class="list-group-item" method="get" action="Search.jsp" >
<input type="hidden" name="item" value="<%=1%>">
...
...
</form>
OR
或者
You can ajaxifyyour code (meaning submit the form through ajax and then redirect).
您可以使用ajaxify您的代码(意思是通过 ajax 提交表单然后重定向)。
<form class="list-group-item" method="get" action="Search.jsp" name="search" >
<input type="hidden" name="item" value="<%=1%>">
...
...
</form>
<script>
$(function(ev){
ev.preventDefault();
$("form:[name='search']").on("submit", function(){
var form = $(this);
var data = form.serialize();
var action = form.attr("action");
$.get(action, data)
window.location.replace("http://stackoverflow.com");
});
});
</script>
回答by Braj
Simply return truefrom the myFunctionand use <input type="submit">to submit the form.
只需return true从myFunction和 用于<input type="submit">提交表单。
Sample code: (read inline comments)
示例代码:(阅读内嵌注释)
<script type="text/javascript">
function myFunction() {
/* return false in case of validation fail */
alert("Hi");
return true;
}
</script>
<form class="list-group-item" action="Search.jsp" method="get"
onsubmit="return myFunction()">
<!-- other fields -->
<input type="submit" value="Submit" />
</form>

