当用户离开一个选项卡时,这段代码将遍历您要离开的选项卡上的每个表单,并像在代码中那样发送一个Ajax请求。区别在于它根据表单的
action
属性,而不必在switch语句中指定它。这意味着
PersonalForm
的action属性必须为
User/Personal
等等。
select: function(e, ui) {
var tab_index = $('#tabs').tabs('option', 'selected');
var panel_id = $("ul li a:eq(" + tab_index + ")", this).attr("href");
var panel = $(panel_id); //the content of the tab
//For each form in the panel, submit it via AJAX and update its html according to the answer
$(panel).find("form").each(function() {
var that = this;
$.post( $(this).attr("action"), $(this).serialize(), function(data, success) {
if (success) {
$(that).html(data);
}
});
});
}
当然,如果页面上没有多个表单,可以跳过
each
如果只有某些表单应该以这种方式提交,则可以向它们添加一个类来筛选所选内容。