• 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重复获取表单值操作

  • 相关阅读:
    Array的应用
    事物的操作
    定义集合
    wxWidgets 在 Linux 下开发环境配置
    Emacs 中 GDB 的使用
    wxWidgets 在 Windows 下开发环境配置
    Ubuntu14.04终端主机名+用户名修改配色方案
    S5PV210之内外存学习
    Ubuntu14.04进行配置符号链接arm2009q3.tar.bz2
    Ubuntu14.041+VMware12.0NET方式网卡连接虚拟机联网问题解决方法
  • 原文地址:https://www.cnblogs.com/dscs/p/5535206.html
Copyright © 2020-2023  润新知