Jquery to change form action

后端 未结 7 1652
孤城傲影
孤城傲影 2020-11-30 23:45

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 t

相关标签:
7条回答
  • 2020-12-01 00:05

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

    Quoting 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>
    
    0 讨论(0)
  • 2020-12-01 00:09

    Try this:

    $('#button1').click(function(){
       $('#formId').attr('action', 'page1');
    });
    
    
    $('#button2').click(function(){
       $('#formId').attr('action', 'page2');
    });
    
    0 讨论(0)
  • 2020-12-01 00:10

    Use jQuery.attr() in your click handler:

    $("#myform").attr('action', 'page1.php');
    
    0 讨论(0)
  • 2020-12-01 00:13

    For variety's sake:

    var actions = {input1: "action1.php", input2: "action2.php"};
    $("#input1, #input2").click(function() {
        $(this).closest("form").attr("action", actions[this.id]);
    });
    
    0 讨论(0)
  • 2020-12-01 00:15
    $('#button1').click(function(){
    $('#myform').prop('action', 'page1.php');
    });
    
    0 讨论(0)
  • 2020-12-01 00:16

    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.

    $('#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);
    });
    
    0 讨论(0)
提交回复
热议问题