jQuery Jquery改变表单动作

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

Jquery to change form action

jquery

提问by nikhil

I have two buttons in a form and two different pages have to be called when they are clicked. when button1 is clicked then page1 must be loaded and when button2 is clicked then page2 must be loaded. i know how to do this in javascript but i have no clue about how to do this in jquery.Can any one help me?

我在一个表单中有两个按钮,单击它们时必须调用两个不同的页面。单击 button1 时必须加载 page1,单击 button2 时必须加载 page2。我知道如何在 javascript 中执行此操作,但我不知道如何在 jquery 中执行此操作。有人可以帮助我吗?

回答by EmCo

Try this:

尝试这个:

$('#button1').click(function(){
   $('#formId').attr('action', 'page1');
});


$('#button2').click(function(){
   $('#formId').attr('action', 'page2');
});

回答by Edwin V.

jQuery is just JavaScript, don't think very differently about it! Like you would do in 'normal' JS, you add an event listener to the buttons and change the action attribute of the form. In jQuery this looks something like:

jQuery 只是 JavaScript,别想太多!就像您在“普通”JS 中所做的那样,您向按钮添加一个事件侦听器并更改表单的 action 属性。在 jQuery 中,这看起来像:

$('#button1').click(function(){
   $('#your_form').attr('action', 'http://uri-for-button1.com');
});

This code is the same for the second button, you only need to change the id of your button and the URI where the form should be submitted to.

此代码与第二个按钮相同,您只需要更改按钮的 id 和表单应提交到的 URI。

回答by Jesse Bunch

Use jQuery.attr()in your click handler:

使用jQuery.attr()您的单击处理程序:

$("#myform").attr('action', 'page1.php');

回答by karim79

For variety's sake:

为了多样化:

var actions = {input1: "action1.php", input2: "action2.php"};
$("#input1, #input2").click(function() {
    $(this).closest("form").attr("action", actions[this.id]);
});

回答by Fernando Vieira

Please, see this answer: https://stackoverflow.com/a/3863869/2096619

请看这个答案:https: //stackoverflow.com/a/3863869/2096619

Quoting Tamlyn:

引用Tamlyn

jQuery (1.4.2) gets confused if you have any form elements named "action". You can get around this by using the DOM attribute methods or simply avoid having form elements named "action".

<form action="foo">
  <button name="action" value="bar">Go</button>
</form>

<script type="text/javascript">
  $('form').attr('action', 'baz'); //this fails silently
  $('form').get(0).setAttribute('action', 'baz'); //this works
</script>

如果您有任何名为“action”的表单元素,jQuery (1.4.2) 会感到困惑。您可以通过使用 DOM 属性方法来解决这个问题,或者简单地避免将表单元素命名为“action”。

<form action="foo">
  <button name="action" value="bar">Go</button>
</form>

<script type="text/javascript">
  $('form').attr('action', 'baz'); //this fails silently
  $('form').get(0).setAttribute('action', 'baz'); //this works
</script>

回答by Jo?o Mello

$('#button1').click(function(){
$('#myform').prop('action', 'page1.php');
});

回答by Gampesh

To change action value of form dynamically, you can try below code:

要动态更改表单的操作值,您可以尝试以下代码:

below code is if you are opening some dailog box and inside that dailog box you have form and you want to change the action of it. I used Bootstrap dailog box and on opening of that dailog box I am assigning action value to the form.

下面的代码是,如果您正在打开某个 dailog 框并且在该 dailog 框中您有表单并且您想要更改它的操作。我使用了 Bootstrap dailog 框,并在打开该 dailog 框时为表单分配了操作值。

$('#your-dailog-id').on('show.bs.modal', function (event) {
    var link = $(event.relatedTarget);// Link that triggered the modal
    var cURL= link.data('url');// Extract info from data-* attributes
    $("#delUserform").attr("action", cURL);
});

If you are trying to change the form action on regular page, use below code

如果您尝试更改常规页面上的表单操作,请使用以下代码

$("#yourElementId").change(function() { 
  var action = <generate_action>;
  $("#formId").attr("action", action);
});