3

I have the a like this one

<form id="popisgolubova_form">
    <input name="pregledaj" type="button" formaction="uredigoluba.php" formmethod="post" formtarget="_self" value="pregledaj" class="button" onclick="popisgolubova_radiobutton(this)">
    <input name="rodovnik" type="button" formaction="rodovnik.php" formmethod="post" formtarget="_blank" value="rodovnik" class="button" onclick="popisgolubova_radiobutton()">
    <input name="podaci" type="button" value="poodaci" formaction="podaciogolubu.php" formmethod="post" formtarget="_blank" class="button" onclick="popisgolubova_radiobutton()">
</form>

and javascript

function popisgolubova_radiobutton(element)
{
    alert($(element).find("[formaction]").val());
    var popisgolubova_radiobutton=$("input[name=RadioGroup1]").is(":checked");
    if(popisgolubova_radiobutton==false)
    {
        alert("nop");
    }
    else
    {
        $("form#popisgolubova_form").submit();
    }   
}

First I'm checking if any checkbox is checked or not and if it is the I can submit the form. But the problem is formaction, formmethod and formtarget. how to get them and submit them

Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
FosAvance
  • 2,363
  • 9
  • 36
  • 52
  • Your html markup is wrong, where is the checkbox? sounds like `html` is from other page and 'script` is from other page. – Dhaval Marthak Jun 20 '13 at 13:31
  • I left out checkboxes because it is working so I just left them out. I need answer how to get that formaction – FosAvance Jun 20 '13 at 13:56

5 Answers5

13

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');
}); 
Mark Broadhurst
  • 2,675
  • 23
  • 45
karthi
  • 889
  • 12
  • 24
  • I'm not sure it is going to work cause I need to get formaction and formmethod from that button, not from whole form – FosAvance Jun 20 '13 at 13:46
2

Hope this helps to get an idea to solve ur problem

<form id="popisgolubova_form">
<input name="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'));
});
Uma
  • 59
  • 8
0

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'));
    }
  });
0
if( $(':focus').is('[formaction]') ) {
    console.log($(':focus').attr('formaction'));
}
Craig
  • 1
0

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.