js获得Form表单内的值

js获得Form表单内的值

1.返回的是一个数组

function getFormData(eId) {
    var inData = new Array();
    $("#" + eId).find("input").each(function () {
        if ($(this).attr("real-value") != null) {
            inData.push({"name": $(this).attr("name"), "value": $(this).attr("real-value").trim()});
        } else {
            inData.push({"name": $(this).attr("name"), "value": $(this).val().trim()});
        }
    });
    $("#" + eId).find("select").each(function () {
        inData.push({"name": $(this).attr("name"), "value": $(this).val().trim()});
    });
    $("#" + eId).find("textarea").each(function () {
        inData.push({"name": $(this).attr("name"), "value": $(this).val().trim()});
    });
    return inData;
}

2.返回的是一个对象

function getFormData(eId) {
    var inData={};
    $("#" + eId).find("input").each(function() {
        if ($(this).attr("real-value") != null) {
            inData[$(this).attr("name")] = $(this).attr("real-value").trim();
        } else {
            inData[$(this).attr("name")] = $(this).val().trim();
        }
    });

    $("#" + eId).find("select").each(function() {
        inData[$(this).attr("name")] = $(this).val();
    });
    $("#" + eId).find("textarea").each(function() {
        inData[$(this).attr("name")] = $(this).val().trim();
    });
    return inData;
};

猜你喜欢

转载自www.cnblogs.com/jcjssl/p/9580265.html
今日推荐