标签:下拉 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能不能同时捕捉点击事件和按键事件?能不能再优化下,有代码。
标签:下拉 amp checkbox property hosts index input attr hang
原文地址:http://www.cnblogs.com/nmnm/p/7145780.html