基於jQuery, 可多選/可過濾重複/可限制個數

來源:互聯網
上載者:User

基於jquery, 可多選/可過濾重複/可限制個數. 是以前的項目中用到過的一個項目, 當初是用原生js東拼西湊的, 用jquery重寫了下,已封裝成外掛程式.暫時沒有對手動輸入的做判斷, 需要的話, 可自行在keyup及change事件中添加相關判斷.

<style>
#demo input{_margin-top:1px;padding-left:5px;border:1px solid #999;width:700px;height:20px;font-size:14px;color:#000}
#dropbox{display:none;z-index:9999;padding:5px;background:#fff;border:1px solid #999;border-top:0;z-index:999}
#dropbox a{margin-right:8px;text-decoration:none}
#dropbox a:hover{text-decoration:underline}
#dropbox p{line-height:24px}
#dropbox em.close{float:right;color:#999;font-style:normal;cursor:pointer}
</style>

<div id="dropbox">
 
 <em class="close" title="關閉">關閉</em>
 
 <p><strong>溫馨提示:</strong>選擇下面的熱門標籤及使用過的標籤,標籤將自動排文至表單域。</p>
 
 <p><strong>熱門標籤:</strong><a href="網頁特效:void(0)">時光漫步</a><a href="javascript:void(0)">javascript</a><a href="javascript:void(0)">jquery plugin</a><a href="javascript:void(0)">那一年</a><a href="javascript:void(0)">jquery外掛程式</a><a href="javascript:void(0)">簡單</a><a href="javascript:void(0)">星空</a><a href="javascript:void(0)">夢田</a><a href="javascript:void(0)">晴朗</a><a href="javascript:void(0)">mrthink.net</a></p>
 
 <p><strong>您使用過的標籤:</strong><a href="javascript:void(0)">許巍</a><a href="javascript:void(0)">sophie zelmani</a><a href="javascript:void(0)">王菲</a><a href="javascript:void(0)">小娟&山穀裡的居民</a><a href="javascript:void(0)">ke$ha</a><a href="javascript:void(0)">shakira</a><a href="javascript:void(0)">樸樹</a><a href="javascript:void(0)">羅大佑</a></p>
 
</div>
<!--//彈出下拉框-->

<br style="clear:both" />
<br style="clear:both" />
<br />
<input value="我是用來測試的" style="width:200px" />
<select>
 <option>我是用來測試的下拉式清單</option> 
 <option>00</option> 
 <option>00</option> 
 <option>00</option> 
</select>

</div>

<input value="我是用來測試的" style="width:200px" />
<select>
 <option>我是用來測試的下拉式清單</option> 
 <option>one</option> 
 <option>two</option> 
 <option>three</option> 
</select>

<!--demo end-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script>
/*******************************
 * @author mr.think
 * @author blog http://mrthink.net/
 * @2010.11.10
 * @可自由轉載及使用,但請註明著作權歸屬
 *******************************/
//調用bgiframe,解決ie6下遮住下拉式清單的問題
//bgiframe函數 copyright (c) 2006 brandon aaron (http://brandonaaron.net)
;(function($){
$.fn.bgiframe = $.fn.bgiframe = function(s) {
 // this is only for ie6
 if ( $.browser.msie && /6.0/.test(navigator.useragent) ) {
  s = $.extend({
   top     : 'auto', // auto == .currentstyle.bordertopwidth
   left    : 'auto', // auto == .currentstyle.borderleftwidth
   width   : 'auto', // auto == offsetwidth
   height  : 'auto', // auto == offsetheight
   opacity : true,
   src     : 'javascript:false;'
  }, s || {});
  var prop = function(n){return n&&n.constructor==number?n+'px':n;},
      html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
                 'style="display:block;position:absolute;z-index:-1;'+
                  (s.opacity !== false?'filter:alpha(opacity='0');':'')+
            'top:'+(s.top=='auto'?'expression(((parseint(this.parentnode.currentstyle.bordertopwidth)||0)*-1)+'px')':prop(s.top))+';'+
            'left:'+(s.left=='auto'?'expression(((parseint(this.parentnode.currentstyle.borderleftwidth)||0)*-1)+'px')':prop(s.left))+';'+
            'width:'+(s.width=='auto'?'expression(this.parentnode.offsetwidth+'px')':prop(s.width))+';'+
            'height:'+(s.height=='auto'?'expression(this.parentnode.offsetheight+'px')':prop(s.height))+';'+
     '"/>';
  return this.each(function() {
   if ( $('> iframe.bgiframe', this).length == 0 )
    this.insertbefore( document.createelement(html), this.firstchild );
  });
 }
 return this;
};
})(jquery);

//選擇標籤至文本域外掛程式
;(function(){
 $.fn.extend({
  iselecttags:function(options){
   var iset={
    name:'.tagsbox',//表單或class或id名
    drop:$('#dropbox'),//彈出框定位
    ps教程eudoclass:$('#dropbox>p>a'),//可選擇的標籤定位
    close:$('em.close'),//關閉按鈕定位
    separator:',',//標籤間分隔字元,建議使用英文逗號
    maxcount:10 //預設限制個數,也可以設定表單的data-count值覆蓋預設值
   }
            options=$.extend(iset, options || {});
   
   var _input=$(iset.name);
   var _inputval=_input.val();
   var _arr=new array(); //存放標籤的數組
   var _left=_input.offset().left; //左絕對距離
   var _top=_input.offset().top+_input.outerheight(); //上絕對距離,此處要加上表單的高度
   var _dropw=_input.outerwidth()-parseint(_input.css教程('border-left-width'))-parseint(_input.css('border-right-width'))-parseint(iset.drop.css('paddingleft'))-parseint(iset.drop.css('paddingright'));
   iset.drop.css({'position':'absolute','left':_left+'px','top':_top+'px','width':_dropw+'px'});
   //彈出框的寬度,此處計算的是與表單實際寬度相等的.也可以直接在樣式中定義.
   var _txt=null;
   var _maxcount=parseint(_input.attr('data-count'));//限制選擇個數
   if(isnan(_maxcount)){
    _maxcount=iset.maxcount
   }
   
   _input.click(function(){
    iset.drop.show();
    iset.drop.bgiframe();//調用bgiframe外掛程式,解決ie6下select的z-index無限大問題
            }).bind('keyup change',function(){
    //可以在此處擴充手動輸入標籤情況下的相關判斷
    //if語句可避免清空重新選擇時第一個字元為逗號
    if ($(this).val() == '') {
     _arr = new array();
    }else {
     _arr = $(this).val().split(iset.separator);//當使用者手動刪除或修改標籤值後更新標籤值
    }
   });
   
   $(document).click(function(e){
    //點擊非彈出框地區時關閉彈出框
    //下面的 if語句是用來判斷傳入的是class還是id
    if(iset.name.charat(0)=='#'){
     if(e.target.id!=iset.name.substring(1)){
      iset.drop.hide();
      }
    }else if(iset.name.charat(0)=='.'){
     if(e.target.classname!=iset.name.substring(1)){
      iset.drop.hide();
      }
    }
   });
   
   iset.drop.click(function(e){
    //阻止彈出框地區預設事件
    e.stoppropagation();
   });
   
   iset.pseudoclass.click(function(){
    //標籤選擇
    _txt=$(this).text();
    //下面的$.inarray是用來判斷是否重複
    //若想反饋重複提示或走出限制個數提示,可改進下面的if語句
    if(($.inarray(_txt,_arr)==-1) && (_arr.length<_maxcount )){
     _arr.push(_txt);
     _inputval=_arr.join(iset.separator);
     _input.val(_inputval);
    }
    
   });
   //關閉按鈕
   iset.close.click(function(){
    iset.drop.hide();
   });
  }
 });
})(jquery);

//外掛程式調用
$(function(){
 $('#demo').iselecttags();
 
 /*
  * 還可以傳入參數調用
  $('#test').iselecttags({
  name:'#test',
  drop:$('.drop'),
  ...
 });
  */
  
});
 

 
  

</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.