Skip to content Skip to sidebar Skip to footer

Jquery Get Formaction And Formmethod

I have the a like this one

Solution 1:

To get the action or method attributes of a form you can try something like below:

$(function() { 
    var action = $("#formid").attr('action'),
        method = $("#formid").attr('method');
}); 

Solution 2:

Hope this helps to get an idea to solve ur problem

<form id="popisgolubova_form">
<inputname="pregledaj"type="button"formaction="uredigoluba.php"formmethod="post"formtarget="_self"value="pregledaj"class="button postForm"/>
</form>

$(document).on('click', '.postForm', function () {

$('#popisgolubova_form').attr('action', $(this).attr('formaction'));
$('#popisgolubova_form').attr('method', $(this).attr('formmethod'));
$('#popisgolubova_form').attr('formtarget', $(this).attr('formtarget'));
});

Solution 3:

So the question is talking about the unfortunately named

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction

...which is a way to override

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-action

per clicking a properly set up submit button. The place you want to check this is upon submit - when you're sure things are actually being submitted.

The button you used to submit is stored as :focus - this does not seem to be otherwise stored in the event object at the moment.

$('form').on("submit", function(event) {
  if( $(':focus').is('[formaction]') ) {
    console.warn($(':focus').attr('formaction'));
    }

  if( $(':focus').is('[formtarget]') ) {
    console.warn($(':focus').attr('formtarget'));
    }
  });

Solution 4:

if( $(':focus').is('[formaction]') ) {
    console.log($(':focus').attr('formaction'));
}

Solution 5:

I had this problem and after searching the web I couldn't find a proper answer. Finally, I realized it's so simple.

When you add an event on form.submit you have an event argument that contains e.originalEvent.submitter, just use it as follows:

$('form').submit(function(e){
    var url = form.attr('action');
    if (e.originalEvent.submitter) {
        var frmAction = $(e.originalEvent.submitter).attr('formaction');
        if (frmAction)
            url = frmAction;
    }
    ,.....
});

You can use the samething for the formmethod as well.

Post a Comment for "Jquery Get Formaction And Formmethod"