[英]Onclick form submit as button submit
I have a link with onclick attr to submit my form. 我有一个与onclick attr链接的表单。 Like this:
像这样:
<a href="javascript:void(0);" onclick="myFunc()">lorem ipsum</a>
function myFunc() {
$('.ajaxform').submit();
}
The problem is.. I'm using an ajax function to do a non-refresh submit.. And I have another script like this: 问题是..我正在使用ajax函数进行非刷新提交..还有另一个类似这样的脚本:
jQuery(document).ready(function(){
jQuery('.ajaxform').submit( function() {
$.ajax({
url : $(this).attr('action'),
type : $(this).attr('method'),
dataType: 'json',
data : $(this).serialize(),
success : function( data ) {
for(var id in data) {
jQuery('#' + id).html( data[id] );
}
}
});
return false;
});
});
If I use a input button type submit the ajax works.. But it doesnt work using the onclick submit... How can I solve this? 如果我使用输入按钮类型,则提交ajax可以正常工作。但是使用onclick提交则不能正常工作。如何解决此问题?
Thanks. 谢谢。
If you are using a <a>
attribute then instead of inline onclick
attribute write the click
function seperately and then use event.preventDefault();
如果您使用的是
<a>
属性,那么click
分别编写click
函数,然后使用event.preventDefault();
代替内联onclick
属性event.preventDefault();
and then submit the form 然后提交表格
<a id='someId' href="javascript:void(0);" onclick="myFunc()">Submit</a> //Give some id
$(function(){
$('#someId').click(function(){
event.preventDefault(); //This will prevent the default action of <a> attribute
$('.ajaxform').submit(function(){
//write the functionality here
});
});
});
Option 1 You can add css to a submit button to look like a link and replace it with your link. 选项1您可以将CSS添加到“提交”按钮中,使其看起来像一个链接,然后将其替换为链接。
Option 2 选项2
HTML 的HTML
<form id="form1" method="POST" action="#" >
<input type="text" class="youwant" name="youwant" id="youwant" />
<a href="#" class="myform" id="myform">lorem ipsum</a>
</form>
JavaScript 的JavaScript
jQuery(document).ready(function(){
$('#myform').on("click", function(event) {
$.ajax({
url : $(this).attr('action'),
type : $(this).attr('method'),
dataType: 'json',
data : $(this).serialize(),
success : function( data ) {
for(var id in data) {
jQuery('#' + id).html( data[id] );
}
}
});
return false;
});
});
I didn't try it but it may help you, just give a try and do reply if helpful. 我没有尝试过,但是它可能会对您有所帮助,请尝试并在有帮助的情况下回复。
$(function() {
$("#tbutton").click(function(){
var frmValues = $('#validation').serialize();
$.ajax({
type: $this.attr('method'),
url: "hello.php",
data: frmValues
})
.done(function () {
$("#para").text("Serialized! Input String is " + frmValues);
})
.fail(function () {
$("#para").text("An error occured");
});
event.preventDefault();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.