jQuery 更改表单提交的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16966383/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:09:52  来源:igfitidea点击:

jQuery change URL of form submit

jquery

提问by GrantU

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.

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

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

回答by Joe

Try using this:

尝试使用这个:

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

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.

移动您使用的顺序.preventDefault()可能会解决您的问题。你也没有使用function(e)所以e.preventDefault();没有工作。

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.

它正在工作:http: //jsfiddle.net/TfTwe/1/- 首先,单击“检查操作属性”。关联。你会收到一条警告说undefined。然后单击“设置操作属性”。并单击“检查操作属性”。再次。您将看到表单的 action 属性已正确设置为/test1

回答by Vitya Ivliiev

Send the data from the form:

从表单发送数据:

$("#change_section_type").live "change", ->
url = $(this).attr("data-url")
postData = $(this).parents("#contract_setting_form").serializeArray()
$.ajax
  type: "PUT"
  url: url
  dataType: "script"
  data: postData