admin管理员组文章数量:1394203
I would have thought the form object would have been populated with formmethod and formaction.
Question: Is there a way to get to formaction? Or find out which button in the form was clicked, to access them from the button?
Do I need to rewrite my event handler to catch clicks on the button ($(document).on('click', '.myformbtn', function(e)
) and use var queryString= $(this).parent('form').serialize();
to access the form.
<form>
<button type="submit"
formmethod="POST"
formaction="/mysave">Save</button>
<button type="submit"
formmethod="GET"
formaction="/mydata">Get new data</button>
</form>
$(document).ready(function()
{
$(document).on('submit', 'form', function(e) {
e.preventDefault();
var formaction = $(this).getFormAction();
var queryString= $(this).serialize();
$.ajax({
type: formmethod,
url: formaction,
data: queryString
});
});
});
I have found similar questions and the answer is always $(this).attr('formaction')
which is incorrect as the form does not have that attribute. I'm hoping providing an example of how it would be used will get peoples brains working.
I would have thought the form object would have been populated with formmethod and formaction.
Question: Is there a way to get to formaction? Or find out which button in the form was clicked, to access them from the button?
Do I need to rewrite my event handler to catch clicks on the button ($(document).on('click', '.myformbtn', function(e)
) and use var queryString= $(this).parent('form').serialize();
to access the form.
<form>
<button type="submit"
formmethod="POST"
formaction="/mysave">Save</button>
<button type="submit"
formmethod="GET"
formaction="/mydata">Get new data</button>
</form>
$(document).ready(function()
{
$(document).on('submit', 'form', function(e) {
e.preventDefault();
var formaction = $(this).getFormAction();
var queryString= $(this).serialize();
$.ajax({
type: formmethod,
url: formaction,
data: queryString
});
});
});
I have found similar questions and the answer is always $(this).attr('formaction')
which is incorrect as the form does not have that attribute. I'm hoping providing an example of how it would be used will get peoples brains working.
2 Answers
Reset to default 5Considering your code we can get the button which caused the form submission in following way:
var target = e.originalEvent || e.originalTarget;
var clickedElement = $( target.currentTarget.activeElement);
$(document).ready(function()
{
$(document).on('submit', 'form', function(e) {
e.preventDefault();
var target = e.originalEvent || e.originalTarget;
var clickedElement = $( target.currentTarget.activeElement);
var formaction = $(clickedElement).attr("formaction");
var formmethod = $(clickedElement).attr("formmethod");
alert(" formaction "+formaction);
var queryString= $(this).serialize();
$.ajax({
type: formmethod,
url: formaction,
data: queryString
});
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form>
This is my form with 2 submit buttons<br><br>
<button type="submit"
formmethod="POST"
formaction="/mysave">Save</button>
<button type="submit"
formmethod="GET"
formaction="/mydata">Get new data</button>
</form>
The only solution I've found that works across browsers. I will still award the correct answer to @vijayP since the answer put me down the right path of research. Unless someone es up with a way to access the clicked element, instead of searching for the focused element.
var clickedElement = $(this).find("input[type=submit]:focus" );
// Added to catch enter press, since there will be no focus.
if(clickedElement.length == 0) {
var clickedElement = $(this).find("input[type=submit]" );
}
var formaction = clickedElement.attr("formaction");
var formmethod = clickedElement.attr("formmethod");
Edit
There were too many bugs with this method. Like having to catch keyboard events, other input types or filter submits.
I've reverted back to an onclick solution. In case anyone is curious.
<form class="dc-form">
// Will activate ajax code
<input type="submit" class="dc-form-btn" value="" />
// Will output error to console
<input type="submit" class="" value="" />
</form>
$(document).ready(function()
{
$(document).on('submit', 'form.dc-form', function(e) {
e.preventDefault();
console.log('Invalid form button');
});
$(document).on('click', '.dc-form-btn', function(e) {
e.preventDefault();
var form = $(this).closest("form.dc-form");
var formaction = $(this).attr("formaction");
var formmethod = $(this).attr("formmethod");
var formDataType = $(this).attr("formdatatype");
var queryString = form.serialize();
var error=false;
if(typeof formaction === "undefined" || formaction === '')
{
console.log('missing form action');
error=true;
}
if(typeof formmethod === "undefined" || formmethod === '')
{
console.log('missing form method');
error=true;
}
if(typeof formDataType === "undefined" || formDataType === '')
{
console.log('missing form data type');
error=true;
}
if(error)
{
console.log("Error executing form button.");
console.log(WEB_URI + formaction);
console.log(formmethod);
console.log(formDataType);
console.log(queryString);
return;
}
$.ajax({
type: formmethod,
url: WEB_URI + formaction,
data: queryString,
dataType: formDataType,
success: ajax_response_success,
error: function(err) {/*console.log(err.responseText);*/alert('ajax error');}
});
});
});
本文标签: javascriptHow to get formmethod and formaction from submit buttonStack Overflow
版权声明:本文标题:javascript - How to get formmethod and formaction from submit button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744663527a2618393.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论