提交表单后jQuery页面重定向 [英] jQuery page redirect after submit the form

查看:305
本文介绍了提交表单后jQuery页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的表格

 < form id =add-to-cartname =my- formaction =http://www.example.commethod =post> 
< div class =row flat>
< input type =hiddenname =cartkeyvalue =>
< input type =hiddenname =idvalue =2>
< button type =submitvalue =submitdata-value =2data-name =id>立即购买< / button>
< / div>
< / form>

保存我使用jQuery的表单。所以现在我的代码看起来像这样

 < script type =text / javascript> 
jQuery(document).ready(function(){
jQuery('button')。click(function(){
var Value = jQuery(this).attr('data-value ');
jQuery('[name =id]')。val(Value);
jQuery('#add-to-cart')。submit();
window .location =http://www.page-2.com;
});
});
< / script>

但是在提交表单后不会重定向到另一个页面( http://www.page-2.com )。
所以有人可以告诉我如何在提交表单后重定向表单吗?

更新
对不起我不能使用ajax。我想提交简单的表单。

解决方案

如果您不使用ajax,表单将被提交,而浏览器重定向到表单的动作url。提交之后做重定向的唯一方法是在操作表单的处理程序中执行此操作。然而,您可以添加一个带有您要重定向到的URL的参数。



这个解决方案相对于涉及ajax的解决方案的唯一优点是不需要javascript。无论javascrit是否启用,它都会起作用。


I have a form like this

<form id="add-to-cart" name="my-form" action="http://www.example.com" method="post">
  <div class="row flat">
    <input type="hidden" name="cartkey" value="">
    <input type="hidden" name="id" value="2">
    <button type="submit"  value="submit" data-value="2" data-name="id">Buy Now</button>
  </div>
</form>

To Save the form I have used jQuery. So for now my code looks like this

 <script type="text/javascript">
    jQuery(document).ready(function() {
      jQuery('button').click(function(){
        var Value = jQuery(this).attr('data-value');
        jQuery('[name="id"]').val(Value);
        jQuery('#add-to-cart').submit();
        window.location = "http://www.page-2.com";
        });
    });
  </script>

But here after submit the form is not redirecting to another page(http://www.page-2.com). So can someone kindly tell me how to do redirect the form after submit the form?

Update Sorry here I can't use ajax. I want to submit the form in simple.

解决方案

If you don't use ajax, the form will be submitted, and the browser redirected to the action url of the form. The only way to do a redirection after the submit, is to do it in the handler of the action form. You could however add a parameter with the URL you want to redirect to.

The only advantage of that solution over a solution involving ajax is that no javascript is required. It would work whether javascrit is enabled or not.

这篇关于提交表单后jQuery页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆