最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - redirect to another page using onsubmit or action? - Stack Overflow

programmeradmin1浏览0评论

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...

<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?

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...

<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?

Share Improve this question edited Jun 19, 2014 at 7:37 Jesuraja 3,8444 gold badges25 silver badges49 bronze badges asked Jun 19, 2014 at 7:20 cooljohnycooljohny 7065 gold badges13 silver badges33 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

Add an button inside <form> tag

<input type="submit" value="Submit">

Add action attribute in form tag

action="Search.jsp"

Whenever you click Submit button, the page will be redirected to Search.jsp

1) If you simply want to get it redirected to Search.jsp after form submission, remove all crap JS code from your form and add a simple action attribute in form tag.
2) Action attribute behaves 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 file and then include it.

<form class="list-group-item" method="get" action="Search.jsp"  >
    <input type="hidden" name="item" value="<%=1%>">
    ...
    ...
</form>


OR

You can ajaxify your code (meaning submit the form through ajax and then redirect).

<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.");
    });
});
</script>

Simply return true from the myFunction and use <input type="submit"> to submit the form.

Sample code: (read inline ments)

<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>
发布评论

评论列表(0)

  1. 暂无评论