javascript处理表单示例(javascript提交表单)


/**
 * Generic Form processing js
 * @author Anthony.chen
 */

/**
 * Push button action [btn_action]data into form
 * If there is prescript , run the pre script
 */
"use strict";
//All ajax request are synchronized by default
var ajaxSynchronized = true;
//All ajax request will be unblock by default
var ajaxAutoUnblock = true;

var ajax_action_button = function (e){
 var btn = $(this);
 //Add prescript
 var pre_script;
 if(pre_script = btn.attr("pre_script")){
  var ret = eval(pre_script);
  if(ret==false){
   return false;
  }
 }
 var btn_action = btn.attr('value');
 if(btn_action){
  $(this).closest('form').data('btn_action',
   { name:'btn_action',value:btn_action }
   );
 }
};

/**
 * Update the extra form data in FormElement with Form element, Key and Value
 */
var ajax_update_post_data = function(formEle, k, v){
 var form = $(formEle);
 var post_data = form.data('post_data');
 if( post_data == undefined ){
  post_data = {};
 }

if( v == undefined ){
  delete post_data[k];
 }else{
  post_data[k] = v;
 }
 form.data('post_data',post_data);
 return true;
};

/**
 * Bool Checkbox is special checkbox which needs to keep UNCHECK value
 * And post with ajax form ,the form is in the parent
 */
var bool_checkbox = function(){
 var ipt = $(this);
 var formEle = ipt.closest("form");
 var _check = ipt.prop("checked");
 if(_check){
  ajax_update_post_data(formEle,ipt.attr('name'));
 }else{
  ajax_update_post_data(formEle,ipt.attr('name'),'f');
 }
};

/**
 * Init the spin number
 */
var spin_number = function(){
 var spin = $(this);
 var config = {
  lock:true,
    imageBasePath: '{webpath}/css/images/spin/',
    btnCss: null,
    txtCss: null,
    btnClass:'spin_btn',
 };
 var interval = spin.attr('interval');
 if(interval){
  config.interval = interval;
 }else{
  config.interval = 1;
 }

var min = spin.attr('min');
 if( min ){
  config.min = min;
 }

var max = spin.attr('max');
 if( max ){
  config.max = max;
 }

spin.spin(config);
 return true;
};

/**
 * Init the date input
 */
var date_input = function(){
 var ipt = $(this);
 var config = {
  offset:[4,0],
  selectors:true,
  lang: '{lang}',
  firstDay : 1,
  format: 'yyyy-mm-dd',
 };

 var min = ipt.attr('min');
 if( min ){
  config.min = min;
 }

var min = ipt.attr('min');
 if( min ){
  config.min = min;
 }

ipt.dateinput(config);
 return true;
};


/**
 * Init the timePicker
 */
var time_picker = function(){
 var ipt = $(this);
 var config = { };

var step = ipt.attr("step");
 if( step ){
  config.step = step;
 }

ipt.timePicker( config );
 return true;
};

/**
 * Generic Ajax Form post function
 * If btn_action is set, then add data into form
 * if returi is set, redirect to returi
 * if reload is set, reload
 * else Show block message
 *
 * the form will be validated.
 */
var ajax_form_post = function(e){
 var form = $(this);
 var pre_script;
 if(pre_script = form.attr("pre_script")){
  var ret = eval(pre_script);
  if(ret==false){
   return false;
  }
 }
 var errHint = form.find(".formError").first();
 if(errHint.size() == 0){
  errHint = $("#pageError");
 }
 errHint.text('').hide();

//Cleanup the pageError
 if(!e.isDefaultPrevented()){
  //Hide all .formError
  $.blockUI({ message:"{__('L_PROCESSING')}" });
  var formArray = form.serializeArray();
  var btn_action_data;
  var btn_action;
  if(btn_action_data = form.data('btn_action')){
   formArray.push(btn_action_data);
   form.removeData('btn_action');
   btn_action = btn_action_data.value;
  }else{
   btn_action = '';
  }

console.log('btn action:'+btn_action);
  //Add extra Data
  var post_data;
  if(post_data = form.data('post_data')){
   for (var k in post_data ){
    //if post_data[k] is array,need more to do
    formArray.push( { name:k ,value: post_data[k] } );
   }
   form.removeData('post_data');
  }

  $.post(form.attr('action'), formArray,function(json){
    if($(window).data('blockUI.isBlocked') == 1){
     $.unblockUI();
    }
    if(json.code === true){
     var returi = "";
     var retData = "{__('L_PROCESSED')}!";
     if(json.data){
      retData = json.data; 
     }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgppsz.html