jQuery Chosen通用初始化
一直在用Chosen這個(gè)js插件,其目的就是美化下拉框。github地址:https://harvesthq.github.io/chosen/
no_results_text:"xxxxx"無搜索結(jié)果時(shí)顯示的文本
allow_single_deselect:true 是否允許取消選擇
disable_search: true 是否有搜索框出現(xiàn)
max_selected_options:當(dāng)select為多選時(shí),最多選擇個(gè)數(shù)
/* 功能: Chosen通用初始化 * 創(chuàng)建人:Brian 創(chuàng)建時(shí)間:2016-12-13 */ (function ($j) { var chosenTool = function (el, options) { this.opts = options; this.chosenInit(); this.chose_get_init(); this.chose_mult_set_init(this.opts.hidClassName); this.clickEvent(); return this; } chosenTool.opts = { selectId: '',//selectId hidClassName: '',//隱藏域Class placeholdertxt: '',//select中placeholder文字 noresulttxt: '',//輸入的名稱未查到時(shí)顯示的文字 dataJson: ''//數(shù)據(jù)源 }; $j.fn.myChosenTool = function (opt) { var value, args = Array.prototype.slice.call(arguments, 1); var $jthis = $j(this), data = $jthis.data('chosenTool'), options = $j.extend({}, chosenTool.opts, $jthis.data(), typeof option === 'object' && option); if (typeof option === 'string') { //判斷用戶調(diào)用的方法是否存在 //if ($j.inArray(option, allowedMethods) < 0) { // throw new Error("Unknown method: " + option); //} if (!data) { return; } value = data[option].apply(data, args); if (option === 'destroy') { $jthis.removeData('chosenTool'); } } /*插件外部調(diào)用插件內(nèi)部的方法需要修改成下面形式*/ //if (typeof opt === 'string') { // if (!data) { // return; // } // value = data[opt].apply(data, args); // if (opt === 'destroy') { // $jthis.removeData('chosenTool'); // } //} if (!data) { opt["selectId"] = $j(this).attr("id"); $jthis.data('chosenTool', (data = new chosenTool(this, opt))); } console.log(data); return typeof value === 'undefined' ? this : value; }; chosenTool.prototype.clickEvent = function () { var _this = this; $j("#" + this.opts.selectId).on("change", function () { _this.chose_get_value(); }); }; /*下拉框初始化方法*/ chosenTool.prototype.selectInfoInit = function () { var proOPts = ""; this.opts.dataJson = $j.parseJSON(this.opts.dataJson); $j.each(this.opts.dataJson, function (index, item) { proOPts += "<option value='" + item.ValueField + "'>" + item.TextField + "</option>"; }); $j("#" + this.opts.selectId).append(proOPts); //初始化chosen $j("#" + this.opts.selectId).chosen({ allow_single_deselect: true, //是否允許取消選擇 placeholder_text_multiple: this.opts.placeholdertxt, //多選框沒有選中任何值的時(shí)候 顯示的文字 no_results_text: this.opts.noresulttxt,//無搜索結(jié)果時(shí)顯示的文本 search_contains: true//是否支持模糊搜索 }); }; /*對(duì)象初始化方法*/ chosenTool.prototype.chosenInit = function () { this.selectInfoInit(); }; //私有方法,檢測(cè)參數(shù)是否合法 chosenTool.prototype.isValid = function () { return !this.options || (this.options && typeof this.options === "object") ? true : false; }; //數(shù)據(jù)同步 chosenTool.prototype.chose_get_init = function () { var selectId = this.opts.selectId; $j("#" + this.opts.selectId).chosen().change( function () { $j("#" + selectId).trigger("liszt:updated");//更新下拉框 }); }; //單選select value獲取 chosenTool.prototype.chose_get_value = function () { var selectVal = $j("#" + this.opts.selectId).val(); $j("." + this.opts.hidClassName).val(selectVal); }; //單選select value獲取 chosenTool.prototype.chose_mult_set_init = function () { var values = $j("." + this.opts.hidClassName).val(); if (values && values.indexOf(',') > 0) { var arr = values.split(','); var length = arr.length; var value = ''; for (i = 0; i < length; i++) { value = arr[i]; $j("#" + this.opts.selectId + " [value='" + value + "']").attr('selected', 'selected'); } } else { $j("#" + this.opts.selectId + " [value='" + values + "']").attr('selected', 'selected'); } $j("#" + this.opts.selectId).trigger("liszt:updated"); }; //select text獲取,多選時(shí)請(qǐng)注意 chosenTool.prototype.chose_get_text = function () { return $j("#" + this.opts.selectId + " option:selected").text(); }; })(jQuery);
以上所述是小編給大家介紹的jQuery Chosen通用初始化,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jQuery使用zTree插件實(shí)現(xiàn)可拖拽的樹示例
本篇文章主要介紹了js使用zTree插件實(shí)現(xiàn)可拖拽的樹示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09jquery中each遍歷對(duì)象和數(shù)組示例
jquery中each可用于遍歷對(duì)象和數(shù)組,如需退出each循環(huán)可使回調(diào)函數(shù)返回false,下面有個(gè)示例,大家可以看看2014-08-08jQuery創(chuàng)建平滑的頁(yè)面滾動(dòng)(頂部或底部)
如何創(chuàng)建一個(gè)平滑的滾動(dòng)效果是本文的目的使用jQuery讓您可以滾動(dòng)到你的網(wǎng)頁(yè)的頂部或底部,相當(dāng)不錯(cuò)的一個(gè)效果,感興趣的你可不要錯(cuò)過了哈2013-02-02學(xué)習(xí)從實(shí)踐開始之jQuery插件開發(fā) 菜單插件開發(fā)
從軟件到網(wǎng)站,菜單可以說是無處不在。在傳統(tǒng)應(yīng)用軟件開發(fā)中,一般都有現(xiàn)成的控件可以使用;但是在網(wǎng)頁(yè)開發(fā)時(shí),基本上要靠開發(fā)人員自己動(dòng)手設(shè)計(jì)2012-05-05Jquery+Ajax+PHP+MySQL實(shí)現(xiàn)分類列表管理(下)
本文將采用Jquery+Ajax+PHP+MySQL來實(shí)現(xiàn)一個(gè)客戶分類列表的管理,如何利用Ajax和Json技術(shù)讓用戶操作起來覺得更輕松,感興趣的小伙伴們可以參考一下2015-10-10詳解easyui基于 layui.laydate日期擴(kuò)展組件
這篇文章主要介紹了詳解easyui基于 layui.laydate日期擴(kuò)展組件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07關(guān)于jQuery.ajax()的jsonp碰上post詳解
這篇文章主要介紹了關(guān)于jQuery.ajax()的jsonp碰上post的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07