最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery get formaction and formmethod - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Jun 6, 2018 at 7:20 Douwe de Haan 6,6463 gold badges33 silver badges51 bronze badges asked Jun 20, 2013 at 13:29 FosAvanceFosAvance 2,4699 gold badges40 silver badges54 bronze badges 2
  • Your html markup is wrong, where is the checkbox? sounds like html is from other page and 'script` is from other page. – Dhaval Marthak Commented Jun 20, 2013 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 Commented Jun 20, 2013 at 13:56
Add a comment  | 

5 Answers 5

Reset to default 12

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');
}); 

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'));
});

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

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.

发布评论

评论列表(0)

  1. 暂无评论