Jquery扩展封装与前后端交互


/*HTML界面模型*/
<body>
<div id="CompanyForm">
    <input name="name" type="text">
    <input name="desc" type="text">
    <input name="type" type="text">
    <input name="link" type="text">
    <select name="sample">
        <option value="aaa">aaaa</option>
        <option value="bbb">bbbb</option>
        <option value="ccc">cccc</option>
        <option value="ddd">dddd</option>
        <option value="eee">eeee</option>
    </select>
</div>
</body>

//自定义扩展方法
$.fn.extend({
    //根据类名获父级ID
    getParentIdByClassName: function (classname) {
        return "#" + $(this).parents().filter("." + classname).prop("id");
    },
    //序列化Json
    SerializeToJson: function () {
        var json = {};
        $.each(this.find('input,select,textarea'), function (i) {
            var el = $(this),
              key = el.attr('name'),
              val = el.val();
            if (val !== undefined && val !== null) {
                if (el.is(':checkbox')) {
                    el.prop('checked') && ($.isArray(json[key]) ? json[key].push(val) : json[key] = [val]);
                } else if (el.is(':radio')) { el.prop('checked') && (json[key] = val); } else {
                    json[key] = val;
                }
            }
        });
        return json;
    },
    //绑定下拉列表
    BindSelectData: function (json) {
        $.element = $(this);
        $.each(json, function (index, obj) {
            $.element.append("<option value=\"" + obj.Value + "\">" + obj.Caption + "</option>");
        });
    },
    //绑定下拉列表
    CheckToJsonString: function () {
        $.element = $(this);
        $.myArray = new Array()
        $.each($.element, function (index, obj) { $.myArray.push(obj.value) });
        return JSON.stringify($.myArray);
    },
    //页面赋值
    SetInputValueAtControl: function (json) {
        for (var s in json) {
            if ($(this).find("[name='" + s + "']").length > 0)
            { $(this).find("[name='" + s + "']").val(json[s]); }
        }
    }
});
//自定义函数
jQuery.CommonFunc = {
    //模板创建
    CreateHtmlByTemp: function (json, tempStr) {
        var newStr = "";
        if (Object.prototype.toString.call(json) === '[object Array]') {
            for (var i = 0; i < json.length; i++) {
                var newStrP = tempStr;
                for (var s in json[i]) { newStrP = newStrP.replace(eval("/{" + s + "}/ig"), json[i][s] == null ? "&nbsp;" : json[i][s]); }
                newStr += newStrP;
            }
        } else {
            var newStrP = tempStr;
            for (var s in json) { newStrP = newStrP.replace(eval("/{" + s + "}/ig"), json[s] == null ? "&nbsp;" : json[s]); }
            newStr += newStrP;
        }
        return newStr;
    },
    //获取URL参数
    GetQueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    },
    //页面赋值
    SetInputValue: function (json) {
        for (var s in json) {
            if ($("[name='" + s + "']").length > 0)
            { $("[name='" + s + "']").val(json[s]); }
        }
    }
}

//前端拼组传给后端的数据
$.formdata = escape(JSON.stringify($("#CompanyForm").SerializeToJson()));                
$.post("/user",$.formdata,function(data){

});
//后台接收解析
BaseCompany BaseCompany = 
JsonConvert.DeserializeObject<BaseCompany>(HttpUtility.UrlDecode(data));

 

posted @ 2019-12-05 00:16  全栈攻城师  阅读(204)  评论(0)    收藏  举报