可輸入可選擇的input


因項目需要 要實現一個可選擇可輸入的下拉框,故寫了一個

html代碼

 1 <div class="col-sm-10">
 2         <input type="text" id="" class="form-control" placeholder="請輸入姓名">
 3     </div>
 4     <div class="col-sm-10">
 5         <input type="text" id="h-input" class="form-control" placeholder="請輸入成績或選擇身高">
 6         <div class="selInput" id="height">
 7             <ul>
 8                 <li>163cm</li>
 9                 <li>162cm</li>
10             </ul>
11         </div>
12     </div> 
13     <div class="col-sm-10">
14         <input type="text" id="w-input" class="form-control" placeholder="請輸入或選擇體重">
15         <div class="selInput" id="weight">
16             <ul>
17                 <li>50kg</li>
18                 <li>40kg</li>
19             </ul>
20         </div>
21     </div> 

css代碼:

 1 .col-sm-10{
 2             position: relative;
 3         }
 4         input{
 5             display: block;
 6             width: 130px;
 7             height: 30px;
 8             line-height: 30px;
 9         }
10         .selInput{
11             display: none;
12             position: absolute;
13             top: 30px;
14             left:8px;
15             width: 130px;
16             line-height: 30px;
17             z-index: 2;
18             background: #fff;
19         }
20         .selInput ul{
21             padding: 0;
22             margin: 0;
23             list-style: none;
24         }

第一次嘗試的時候以為只要判斷input的點擊和ul的顯示與隱藏就行,結果發現出了問題,也把第一次的代碼貼出來好做對比

 1 function ChoseAndInput(i,u){
 2         this.i = i;
 3         this.u = u;
 4     }
 5     ChoseAndInput.prototype = {
 6         canInput:function(){
 7           $(this.i).click(function(){
 8             $(this.u).show();
 9             $(this.i).bind('input propertychange',function(){ //給input綁定oninput onpropertychange事件 判斷input框的值是否改變 如果改變就隱藏ul
10               $(this.u).hide();
11             }.bind(this))
12             console.log(123)
13           }.bind(this))
14 
15         },
16         canChose:function(){
17           $(this.u).click(function(e){
18             var val = $(e.target).text();
19             $(this.i).val(val);
20             $(this.u).hide();
21           }.bind(this))
22         }
23     }
24     var height = new ChoseAndInput('#h-input',"#height");
25     height.canInput();
26     height.canChose();
27 
28     var weight = new ChoseAndInput('#w-input',"#weight");
29     weight.canInput();
30     weight.canChose();

發現了問題:就是點擊input后ul顯示,但是此時如果不輸入也不選擇就讓input失去了焦點,發現ul就不會隱藏了,這個問題也確實解決了好久,最后發現給document綁定click事件,判斷一下點擊的元素是不是可輸入也可選擇的那個下拉框就好,最終的js代碼如下:

 1 $(function(){
 2         $(document).bind('click', function(e){
 3             var e=e||window.event;//瀏覽器兼容性
 4             var elem = e.target || e.srcElement;
 5             var $sbling = $(elem).next(); //當前input框的下一個兄弟元素
 6             if($sbling && $sbling.attr('class')=='selInput'){//判斷點擊的元素是不是需要選擇或者輸入的下拉框
 7                 $('.selInput').hide(); //下拉框都隱藏
 8                 $sbling.show(); //只有當前input下的下拉框才顯示
 9                 return;
10             }
11             $('.selInput').hide();
12         })
13     })
14     function ChoseAndInput(i,u){
15         this.i = i;
16         this.u = u;
17     }
18     ChoseAndInput.prototype = {
19         canInput:function(){
20           $(this.i).click(function(){
21             $(this.u).show();
22             $(this.i).bind('input propertychange',function(){ //給input綁定oninput onpropertychange事件 判斷input框的值是否改變 如果改變就隱藏ul
23               $(this.u).hide();
24             }.bind(this))
25             console.log(123)
26           }.bind(this))
27 
28         },
29         canChose:function(){
30           $(this.u).click(function(e){
31             var val = $(e.target).text();
32             $(this.i).val(val);
33             $(this.u).hide();
34           }.bind(this))
35         }
36     }
37     var height = new ChoseAndInput('#h-input',"#height");
38     height.canInput();
39     height.canChose();
40 
41     var weight = new ChoseAndInput('#w-input',"#weight");
42     weight.canInput();
43     weight.canChose();

最終結果就是這樣了,功能是實現了,不過應該還有改進的地方,所以寫出來讓大家幫忙看看,提一些意見^_^


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM