admin管理员组文章数量:1394986
I have this code:
$(document).on('submit','#roomsend',function(e) {
var form = $('#roomsend');
var data = form.serialize();
$.post('php/roomlist.php', data, function(response) {
console.log(response);
$('#power').replaceWith(response);
});
return false;
});
This works with a Submit button inside the form. But now I have a button outside the form:
<button class="btn btn-success" id="roomsendbutton"><i class="icon-ok"></i> Save</button>
How must I must be the code, that when i clicked the button the button all actions will be work and the form submits?
I have this code:
$(document).on('submit','#roomsend',function(e) {
var form = $('#roomsend');
var data = form.serialize();
$.post('php/roomlist.php', data, function(response) {
console.log(response);
$('#power').replaceWith(response);
});
return false;
});
This works with a Submit button inside the form. But now I have a button outside the form:
<button class="btn btn-success" id="roomsendbutton"><i class="icon-ok"></i> Save</button>
How must I must be the code, that when i clicked the button the button all actions will be work and the form submits?
Share Improve this question asked May 24, 2013 at 19:32 fibifibi 1491 gold badge7 silver badges13 bronze badges 1- $(document).on('click','#roomsendbutton',function(e){..}) solves your problem i guess – Sri Tirupathi Raju Commented May 24, 2013 at 19:35
4 Answers
Reset to default 4If you're using HTML5, you can use the new form attribute:
<button form="roomsend" type="submit" class="btn btn-success" id="roomsendbutton"><i class="icon-ok"></i> Save</button>
http://jsfiddle/sEHLf/
Should probably look up browser support for it though.
Use .trigger()
method:
$('#roomsendbutton').on('click', function () {
$('#roomsend').trigger('submit');
});
References:
.trigger()
- jQuery API Documentation
Just do something like this in your jQuery..
$('#roomsendbutton').click(function(){
$(your form name).trigger('submit');
});
$(document).on('click','#roomsendbutton',function(e) {
var form = $('#roomsend');
var data = form.serialize();
$.post('php/roomlist.php', data, function(response) {
console.log(response);
$('#power').replaceWith(response);
});
return false;
});
Just bind the click event on the button.
本文标签: javascriptJquery Ajax Submit a form when i click a button which not been in the formStack Overflow
版权声明:本文标题:javascript - Jquery Ajax Submit a form when i click a button which not been in the form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107795a2591146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论