jsp 通用获取所有表单值传后台

 

 

新建一个js文件,自定义一个jquery 函数。 在jsp页面引用

 

 

下面为:自定义函数

$.fn.GetDivJson = function (prifix,orgModel) {
    var $outerWrap = $("<div>");
    $outerWrap.append(this.clone(false));
    var $inputs = $outerWrap.find(":text[name^=" + prifix + "]");
    var $hiddens = $outerWrap.find(":hidden[name^=" + prifix + "]");
    var $pwds = $outerWrap.find(":password[name^=" + prifix + "]");
    var $radios = $outerWrap.find(":radio[name^=" + prifix + "]");
    var $checkBox = $outerWrap.find(":checkbox[name^=" + prifix + "]");
    var $textArea = $outerWrap.find("textarea[name^=" + prifix + "]");
    var $selects = $outerWrap.find("select[name^=" + prifix + "]");
    var json = {};
    $inputs.each(function (i, item) {
    
        if(item.value ==$(item).attr("placeholder")) return true;  //用于IE下判断CJH
        
        json[item.name.replace(prifix, "")] = $.trim(item.value);
    });
    $hiddens.each(function (i, item) {
        
        //alert($.trim(item.value));
        //alert("placeholder:"+$(item).attr("placeholder"));
        
        if($.trim(item.value)==$(item).attr("placeholder")) return true; //用于IE下判断 CJH
        
        json[item.name.replace(prifix, "")] = $.trim(item.value);
    });
    $pwds.each(function (i, item) {
        json[item.name.replace(prifix, "")] =  $.trim(item.value);
    });
    $textArea.each(function (i, item) {
        json[item.name.replace(prifix, "")] =  $.trim(item.value);
    });
    var radios = [];
    $radios.each(function (i, item) {
        var radioName = item.name.replace(prifix, "");
        if (radios.toString().indexOf(radioName) == -1) {
            radios.push(radioName);
            json[radioName] = $radios.filter("[name=" + prifix + radioName + "]:checked").val() || "";
        }
    });
    var checkbox = [];
    $checkBox.each(function (i, item) {
        var chkName = item.name.replace(prifix, "");
        if (checkbox.toString().indexOf(chkName) == -1) {
            checkbox.push(chkName);
            var $nameChk = $checkBox.filter("[name=" + prifix + chkName + "]:checked");
            if ($nameChk.length > 0) {
                var vals = [];
                $nameChk.each(function (j, subItem) {
                    vals.push(subItem.value);
                })
                json[chkName] = vals.join(",");
            }
            else
                json[chkName] = "";
        }
    });

    $selects.each(function (i, item) {
        json[item.name.replace(prifix, "")] = $(item).find("option:selected").val();
    });
    orgModel && (json.oplog = $.compareModel(json,orgModel));    
    return json;
}

 

页面表单input等id 、name,  注意:以"Query_"开头

 <input type="hidden" id="Query_type" name="Query_type" /> 
 <input type="hidden" id="Query_subID" name="Query_subID" />

 

在页面的jquery 代码中   如做jquery 异步提交

$("#searchBtn").click(function (e) {
            var queryModel =$(".Query").GetDivJson("Query_");
             $.post("../Goods/GoodsApply",queryModel,function(data){
                 
            }); 
        });

 

在后台用对象或相应字段接受即可,避免了前端jsp页面   jquery重复获取表单值操作

posted @ 2016-05-27 16:47  大叔陈说  阅读(571)  评论(0编辑  收藏  举报