ajax獲取數據后初始化表單,以及提交表單到后台


當前的代碼是結合jquery使用,拓展了jquery的方法,可以直接通過$("#form1").formSerialize(data);來使用
給$.fn命名空間添加的方法,可以直接使用,這個是直接給jquery拓展的方法;
當前方法formdate如果有數據將會初始化表單中對應的文本框,或者是選擇框;
如果fromdate為空,則是獲取當前表單的所有數據,提交到后台;

$.fn.formSerialize = function (formdate) { var element = $(this); if (!!formdate) { for (var key in formdate) { var $id = element.find('#' + key); var value = $.trim(formdate[key]).replace(/ /g, ''); var type = $id.attr('type'); if ($id.hasClass("select2-hidden-accessible")) { type = "select"; } switch (type) { case "checkbox": if (value == "true") { $id.attr("checked", 'checked'); } else { $id.removeAttr("checked"); } break; case "select": $id.val(value).trigger("change"); break; default: $id.val(value); break; } }; return false; } var postdata = {}; element.find('input,select,textarea').each(function (r) { var $this = $(this); var id = $this.attr('id'); var type = $this.attr('type'); switch (type) { case "checkbox": postdata[id] = $this.is(":checked"); break; default: var value = $this.val() == "" ? " " : $this.val(); if (!$.request("keyValue")) { value = value.replace(/ /g, ''); } postdata[id] = value; break; } }); if ($('[name=__RequestVerificationToken]').length > 0) { postdata["__RequestVerificationToken"] = $('[name=__RequestVerificationToken]').val(); } return postdata; };

//下面的方法將會獲取當前form中的所有數據提交到后台;
function submitForm() {

  if (!$('#form1').formValid()) {
    return false;
  }  
  $.submitForm({
    url: "/ArchivesManage/Manage/SubmitForm?keyValue=" + keyValue,
    param: $("#form1").formSerialize(),
    success: function () {
    $.currentWindow().$("#gridList").trigger("reloadGrid");
    }
  })
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM