js自定义命名空间

<pre name="code" class="html">定义命名空间
 
 
 
var nameSpace = nameSpace || {};
(function () {
    var global = window;
    /**
     * 
     * @param {} nsStr
     * @return {}
     */
    nameSpace.ns = function (nsStr) {
        var parts = nsStr.split("."),
        root = global,
        max,
        i;
        for (i = 0, max = parts.length ; i < max ; i++) {
            //如果不存在,就创建一个属性
            if (typeof root[parts[i]] === "undefined") {
            root[parts[i]] = {};
            }
            root = root[parts[i]];
        }
        return root;
    };
})();



 nameSpace.ns("foodsafe.detect"); //注册命令空间
 foodsafe.detect={};
 
 foodsafe.detect = {
    idx:10000,
    addRow:function(tableName){ //表格增加一行,复制<span style="font-family: Arial, Helvetica, sans-serif;">select2</span>
         var copySelect = $("#copyTable tbody").find("select[name='pointSelected']");//复制<span style="font-family: Arial, Helvetica, sans-serif;">select2下拉框</span>
         copySelect.select2('destroy');
         $("#" + tableName + " tbody").append($("#copyTable tbody tr").clone());
         var pointSelect = $($("#" + tableName + " tr:last").find("select[name='pointSelected']"));
         pointSelect.select2('destroy');
         this.initSelect2(pointSelect);
         pointSelect.select2();
         var indexId = this.idx;
         $("#" + tableName + " tr:last").find("[type='text']").each(function(){
             $(this).attr("name",$(this).attr("name") + "-" + indexId);
         });
         this.idx = this.idx + 1;
    },
    
    initSelect2:function($elem) {
        $elem.select2({
            minimumResultsForSearch: -1,
            width: 'resolve'
        });   
    },
    
    doItemSubmit:function(){
           this.doTr2Joson();
           $("#inputForm").submit();            

    },

    doTr2Joson:function(){  //ajax 行值转josn
        var obj = new Array();
        $("[id^=contentTable]").each(function(i){
            $(this).find("tr").each(function(j){
                if(j > 0){
                    var trObj = $(this);
                    var id = $(trObj.find('input[name^="id"]')).val();
                    var deviceType = i;
                    var pconfig= {
                            id:id,
                            deviceType:deviceType
                    }
                    obj.push(<span style="font-family: Arial, Helvetica, sans-serif;">pconfig</span>);
                }
           });
        });
        $("#ipcJson").val(JSON.stringify(obj));//赋值
    }
 }
</pre><pre name="code" class="html"><ul><li><span style="font-family: Arial, Helvetica, sans-serif;">使用</span></li></ul>
<pre name="code" class="html"><input id="btnSubmit" class="btn btn-primary" type="button" value="保 存" onclick="foodsafe.detect.doItemSubmit()"/>


 
 
<p>
</p>

你可能感兴趣的:(js命名空间,复制select2下拉框)