admin管理员组文章数量:1355660
i try to get the value of one of two submit-buttons from my formular, but i failed in every case i try to do it.
Hope, someone can help me and show me how i can do it.
This is the html form
<form id="new_contact" class="save_form" action="kontaktdetails.php?art='.$_POST['art'].'" method="post" >
<button class="btn" id="submit1" type="submit" name="save_close">Save and close<i class="fa fa-save-right"></i></button>
<button class="btn" id="submit2" type="submit" name="save_next">Save and next one<i class="fa fa-save-right"></i></button>
</form>
This is the JS Code, here i need the name or a id of the clicked button
$(document).on('submit', '.save_form', function (event) {
event.preventDefault();
// var submitbutton = !????
var formID = $(this).attr('id');
var formDetails = $('#'+formID);
var fileurl = $(this).attr('action');
...
i try to get the value of one of two submit-buttons from my formular, but i failed in every case i try to do it.
Hope, someone can help me and show me how i can do it.
This is the html form
<form id="new_contact" class="save_form" action="kontaktdetails.php?art='.$_POST['art'].'" method="post" >
<button class="btn" id="submit1" type="submit" name="save_close">Save and close<i class="fa fa-save-right"></i></button>
<button class="btn" id="submit2" type="submit" name="save_next">Save and next one<i class="fa fa-save-right"></i></button>
</form>
This is the JS Code, here i need the name or a id of the clicked button
$(document).on('submit', '.save_form', function (event) {
event.preventDefault();
// var submitbutton = !????
var formID = $(this).attr('id');
var formDetails = $('#'+formID);
var fileurl = $(this).attr('action');
...
Share
Improve this question
edited Oct 11, 2016 at 19:57
Just Jake
4,8685 gold badges29 silver badges33 bronze badges
asked Oct 11, 2016 at 19:11
4usolutions4usolutions
2791 gold badge4 silver badges13 bronze badges
3
-
Why not do
$(document).on('click', 'button[type="submit"]', function() { // 'this' is button and form information is still available.. });
– Gavin Commented Oct 11, 2016 at 19:14 -
it looks like
$(document)
should be an id selector for the form – robbmj Commented Oct 11, 2016 at 19:14 - @robbmj Why do you suggest that? – Gavin Commented Oct 11, 2016 at 19:16
3 Answers
Reset to default 4Try binding the event to the submit buttons like this:
$('.save_form').on('click', '[type=submit]', function(event) {
event.preventDefault();
var submitbutton = $(this).attr('id');
var formDetails = $(this).closest('form');
var formID = formDetails.attr('id');
var fileurl = formDetails.attr('action');
console.log(submitbutton);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="new_contact" class="save_form" action="kontaktdetails.php?art='.$_POST['art'].'" method="post">
<button class="btn" id="submit1" type="submit" name="save_close">Save and close<i class="fa fa-save-right"></i>
</button>
<button class="btn" id="submit2" type="submit" name="save_next">Save and next one<i class="fa fa-save-right"></i>
</button>
</form>
There are many ways to do this, one would be to add an event handler to the buttons, as the click happens before the submit, and then use a class or something to identify which button was clicked inside the submit handler.
That way you'd still catch and prevent submits that wasn't initiated from the buttons.
If you don't need to catch the submit specifically, you could just use an event handler for the buttons instead of the form submit
$(document).on('click', '.save_form button', function() {
$('.save_form button').removeClass('clicked');
$(this).addClass('clicked');
});
$(document).on('submit', '.save_form', function(event) {
event.preventDefault();
var formID = $(this).attr('id');
var formDetails = $('#' + formID);
var fileurl = $(this).attr('action');
var id = $(this).find('button.clicked').prop('id');
alert(id)
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="new_contact" class="save_form" action="kontaktdetails.php?art='.$_POST['art'].'" method="post">
<button class="btn" id="submit1" type="submit" name="save_close">Save and close<i class="fa fa-save-right"></i>
</button>
<button class="btn" id="submit2" type="submit" name="save_next">Save and next one<i class="fa fa-save-right"></i>
</button>
</form>
let say you have the form bellow,
<form id="sForm">
<input type="text" name="toSearch" id="toSearch" />
<input type="submit" id="mysubmit"/>
</form>
and you want to retrieve to value when the form is submitted.In my example the id of the input is named "toSearch". Just do the following.
$('#sForm').on("submit",function(e){
var myvar = $('#toSearch').val();
//Now you can do anything you wish with the variable myvar
// Put more code here.
e.preventDefault();
});
The value will be retrieved on submit but you can change it to Onclick or any other kind of action.
Hope it will help
本文标签: javascriptget value of clicked submit buttonStack Overflow
版权声明:本文标题:javascript - get value of clicked submit button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743969145a2570441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论