jQuery更改表单提交的URL [英] jQuery change URL of form submit

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

问题描述

我想更改单击后提交表单的URL,如下所示.以下是我尝试过的方法,但是它不起作用.

I want to change the URL the form is submitted to upon click as seen below. Below is what I have tried but it does not work.

<form action="" method="post" class="form-horizontal" id="contactsForm" enctype="multipart/form-data">
  <div class="widget-content">

    <div class="btn-group">
      <button data-toggle="dropdown" class="btn dropdown-toggle">  &nbsp;
        <span class="icon-folder-open icon-large"></span>
        Move To <span class="caret"></span>
      </button>
      <ul class="dropdown-menu">
        {% for x,y in actionsForm.fields.move_to.choices %}
        <li><a class="move_to" id="{{ x }}" href="#">{{ y }}</a></li>
        {% endfor %}

<script>
    $(document).ready(function() {
        $(".move_to").on("click", function () {
            $('#contactsFrom').attr('action', "/test1");
            $("#contactsFrom").submit();
            e.preventDefault();
        });
    });
</script>

推荐答案

尝试使用此功能:

$(".move_to").on("click", function(e){
    e.preventDefault();
    $('#contactsForm').attr('action', "/test1").submit();
});

更改您使用.preventDefault()的顺序可能会解决您的问题.您也没有使用function(e),所以e.preventDefault();无效.

Moving the order in which you use .preventDefault() might fix your issue. You also didn't use function(e) so e.preventDefault(); wasn't working.

在这里正常工作: http://jsfiddle.net/TfTwe/1/-首先,单击检查动作属性".关联.您会收到一条警报,提示undefined.然后点击设置操作属性".然后点击检查操作属性".再次.您会看到表单的action属性已正确设置为/test1.

Here it is working: http://jsfiddle.net/TfTwe/1/ - first of all, click the 'Check action attribute.' link. You'll get an alert saying undefined. Then click 'Set action attribute.' and click 'Check action attribute.' again. You'll see that the form's action attribute has been correctly set to /test1.

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

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