收藏的工具代码

function formToHash(form){
	    var hash = {}, el;
	    for(var i = 0,len = form.elements.length;i < len;i++){
	        el = form.elements[i];
	        if(el.name == "" || el.disabled) continue;
	        switch(el.tagName.toLowerCase()){
	        case "fieldset":
	            break;
	        case "input":
	            switch(el.type.toLowerCase()){
	            case "radio":
	                if(el.checked)
	                    hash[el.name] = el.value;
	                break;
	            case "checkbox":
	                if(el.checked){
	                    if(!hash[el.name]){
	                        hash[el.name] = [el.value];
	                    }else{
	                        hash[el.name].push(el.value);
	                    }
	                }
	                break;
	            case "button":
	                break;
	            case "image":
	                break;
	            default:
	                hash[el.name] = el.value;
	                break;
	            }
	            break;
	        case "select":
	            if(el.multiple){
	                for(var j = 0, lens = el.options.length;j < lens; j++){
	                    if(el.options[j].selected){
	                        if(!hash[el.name]){
	                            hash[el.name] = [el.options[j].value];
	                        }else{
	                            hash[el.name].push(el.options[j].value);
	                        }
	                    }
	                }
	            }else{
	                hash[el.name] = el.value;
	            }
	            break;
	        default:
	            hash[el.name] = el.value;
	            break;
	        }
	    }
	    form = el = null;
	    return hash;
	}

posted on 2011-04-25 10:01  不惑ed  阅读(681)  评论(0编辑  收藏  举报