javascript JQuery 获取 formaction 和 formmethod

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

JQuery get formaction and formmethod

javascriptjqueryforms

提问by FosAvance

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

和 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

首先,我正在检查是否选中了任何复选框,如果是,我可以提交表单。但问题是formaction、formmethod和formtarget。如何获取并提交它们

回答by karthi

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

回答by Uma

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

回答by user6167808

So the question is talking about the unfortunately named

所以问题是关于不幸命名的

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

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

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.

您用于提交的按钮存储为 :focus - 目前这似乎没有存储在事件对象中。

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

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

回答by Craig

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