jquery的表單驗證方法,一個function能不能同時捕捉點擊事件和按鍵事件?能不能再最佳化下,有代碼。

來源:互聯網
上載者:User

標籤:下拉   amp   checkbox   property   hosts   index   input   attr   hang   

// 該jquery擴充引自 http://www.ghostsf.com/tools/389.html 方法名是作者部落格的命名
$.fn.ghostsf_serialize = function () {
  var a = this.serializeArray();
  var $radio = $(‘input[type=radio],input[type=checkbox]‘, this);
  var temp = {};
  $.each($radio, function () {
  if (!temp.hasOwnProperty(this.name)) {
    if ($("input[name=‘" + this.name + "‘]:checked").length == 0) {
    temp[this.name] = "";
    a.push({name: this.name, value: ""});
    }
  }
});
return jQuery.param(a);
};

// 粗製濫作的 表單判空
function verify2(valbtnId,valformId) {

  $(valbtnId).attr(‘disabled‘,true);
  var reg = /^\s*$/g;
  $(valformId).keyup(‘change‘,function valkeyup(){

  //var formdataarray = $(valformId).serialize().split(‘&‘); 未擷取到未選中的 checkbox radio

  //使用擴充後 ghostsf_serialize() 方法可以擷取
  var formdataarrayall = $(valformId).ghostsf_serialize().split(‘&‘);
  var countf = 0;

  for(var i = 0; i < formdataarrayall.length; i++) {

    var labeltype = formdataarrayall[i].substring(0,formdataarrayall[i].indexOf(‘=‘));

    if ($(‘*[name=‘+labeltype+‘]‘).is(‘select‘)) {
    //如果是下拉框 觸發鍵盤事件 ?
      if($(‘select[name=‘+labeltype+‘]‘).children(‘options:selected‘)){
        if (!reg.test($(‘select[name=‘+labeltype+‘]:selected‘).val())) {
        countf++;
        }
      }
    }

    if ($(‘*[name=‘+labeltype+‘]‘).is(‘input‘)) {
      if($(‘*[name=‘+labeltype+‘]‘).attr(‘type‘) == ‘text‘){
        if (!reg.test($(‘input[name=‘+labeltype+‘]‘).val())) {
        countf++;
        }
      }
      if ($(‘*[name=‘+labeltype+‘]‘).attr(‘type‘) == ‘checkbox‘) {
        if ( !reg.test($(‘input[name=‘+labeltype+‘]:checked‘).val()) && $(‘input[name=‘+labeltype+‘]:checked‘).val()!= undefined) {
        countf++;
        }
      }
      if ($(‘*[name=‘+labeltype+‘]‘).attr(‘type‘) == ‘radio‘) {
        if (!reg.test($(‘input[name=‘+labeltype+‘]:checked‘).val()) && $(‘input[name=‘+labeltype+‘]:checked‘).val()!= undefined) {
        countf++;
        }
      }
    }

  }
  valreturn(countf == i ? false : true,valbtnId);
});

$(valformId).change(‘click‘,function valchange(){
    var formdataarrayall = $(valformId).ghostsf_serialize().split(‘&‘);
    var countf = 0;
  for(var i = 0; i < formdataarrayall.length; i++) {

    var labeltype = formdataarrayall[i].substring(0,formdataarrayall[i].indexOf(‘=‘));

    if ($(‘*[name=‘+labeltype+‘]‘).is(‘select‘)) {

      if($(‘select[name=‘+labeltype+‘]‘).children(‘options:selected‘)){
        if (!reg.test($(‘select[name=‘+labeltype+‘]‘).val())) {
        countf++;
        }
      }
    }

    if ($(‘*[name=‘+labeltype+‘]‘).is(‘input‘)) {
        //alert($(‘*[name=‘+labeltype+‘]‘).prop(‘type‘));
        if($(‘*[name=‘+labeltype+‘]‘).attr(‘type‘) == ‘text‘){
          if (!reg.test($(‘input[name=‘+labeltype+‘]‘).val())) {
          countf++;
          }
        }
      if ($(‘*[name=‘+labeltype+‘]‘).attr(‘type‘) == ‘checkbox‘) {
        if ( !reg.test($(‘input[name=‘+labeltype+‘]:checked‘).val()) && $(‘input[name=‘+labeltype+‘]:checked‘).val()!=undefined) {
        countf++;
        }
      }
      if ($(‘*[name=‘+labeltype+‘]‘).attr(‘type‘) == ‘radio‘) {
        if (!reg.test($(‘input[name=‘+labeltype+‘]:checked‘).val()) && $(‘input[name=‘+labeltype+‘]:checked‘).val()!=undefined) {
        countf++;
        }
      }
    }
  }
  valreturn(countf == i ? false : true,valbtnId);
});

}

function valreturn(val,valbtnId){
    $(valbtnId).attr(‘disabled‘,val);
    return val;
}

 

jquery的表單驗證方法,一個function能不能同時捕捉點擊事件和按鍵事件?能不能再最佳化下,有代碼。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.