js

获取表单内容(一个方法获取所有表单的值)

2017-11-15  本文已影响4人  悠哉悠哉过日子

//获取表单内容,要求id把#传过来 如果传餐 "#fromId"

function getFormValues(formId){

    var params={};

    $(formId).find("input[type='text']").each(function(i) {

        var id = this.id;

        var value = this.value;

        params[id] = value;

    });

    $(formId).find("input[type='hidden']").each(function(i) {

        var id = this.id;

        var value = this.value;

        params[id] = value;

    });

    $(formId).find("input[type='password']").each(function(i) {

        var id = this.id;

        var value = this.value;

        params[id] = value;

    }); 

    $(formId).find("textarea").each(function(i) {

        var id = this.id;

        var value = this.value;

        params[id] = value;

    });

    $(formId).find("select").each(function(i) {

        var id = this.id;

        var value = $("#"+this.id).find("option:selected").text();

        params[id] = value;

    });

    $(formId).find("input[type='number']").each(function(i) {

        var id = this.id;

        var value = this.value;

        params[id] = value;

    });

    return params;

}

上一篇 下一篇

猜你喜欢

热点阅读