Js一次性获取表单内所有的值

//  frmID为表单的id
function getFormQueryString(frmID) {
        var frmID = document.getElementById(frmID);
        var i, queryString = "", and = "";
        var item; // for each form's object
        var itemValue;// store each form object's value

        for (i = 0; i < frmID.length; i++) {
            item = frmID[i];// get form's each object
            if (item.name != '') {
                if (item.type == 'select-one') {
                    itemValue = item.options[item.selectedIndex].value;
                } else if (item.type == 'checkbox' || item.type == 'radio') {
                    if (item.checked == false) {
                        continue;
                    }
                    itemValue = item.value;
                } else if (item.type == 'button' || item.type == 'submit' || item.type == 'reset' || item.type == 'image') {// ignore this type
                    continue;
                } else {
                    itemValue = item.value;
                }
                itemValue = encodeURIComponent(itemValue);
                queryString += and + item.name + '=' + itemValue;
                and = "&";
            }
        }
        alert(queryString);
        return queryString;
    }

你可能感兴趣的:(Js一次性获取表单内所有的值)