您的位置:首页 > Web前端 > JavaScript

js自定义命名空间

2016-04-29 16:08 579 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: