![](/img/trans.png)
[英]PHP foreach prints out forms | all forms are submitted to one ajax code, but just the first form works
[英]One code to turn all forms into AJAX forms?
因此,我使用以下代码来处理我的“登录”表单,以便将其提交到action =“”的url,并且响应的数据将进入target =“”指定的ID。 提交方法由method =“”指定
我喜欢这个。 干净利落 但是我想知道是否有一种方法可以使所有表单都通过Jquery提交,而不必每次都为每个表单重新编写此代码。 太乱了
非常感激!
<script type="text/javascript">
$(document).ready(function(){
$("#login").submit(function(){
var thistarget = this.target;
$(thistarget).html('Submitting...');
jQuery.ajax({
data: $(this).serialize(),
url: this.action,
type: this.method,
error: function() {
$(thistarget).html("Form failed to submit");
},
success: function(results) {
$(thistarget).html(results);
}
})
return false;
}
);
});
</script>
当然,只需将"#login"
替换为"form"
。 然后,它将影响页面上当前存在的所有表单(但不会影响将来的表单)。
$("#login").submit(function(){...
对于将来的表格和当前的表格,您将需要事件委托。
$(document).on("submit","form",function(){...
如果我是我,我会使用Jquery的$ .post方法。 http://api.jquery.com/jQuery.post/但请回答您的问题,正如Kevin B所说:通过表单更改#login
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.