javaScript代码小集合将form转为AJAX提交

//将form转为AJAX提交
function do_ajax_submit_form(formid, fn) {
	frm = $("#"+formid);
	var dataPara = get_form_json(frm);
	$.ajax({
		url : frm.attr("action"),
		type : method = frm.attr("method"),
		data : dataPara,
		success : fn
	});
}
//ajax请求
function do_ajax_post(url, data,fn) {
	$.ajax({
		url : url,
		type : "POST",
		data : data,
		success : fn
	});
}
// 将form中的值转换为键值对。
function get_form_json(frm) {
	var o = {};
	var a = $(frm).serializeArray();
	$.each(a, function() {
		if (o[this.name] !== undefined) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});

	return o;
}

相关推荐