問題描述
下面是我的代碼我的問題是我只想像劍道一樣實現我的意思是當用戶鍵入文本時不選擇文本,如果我們不選擇文本,應該從下拉列表中選擇它,就像
$(function(){$(".chosen-select").chosen();});
<link rel="stylesheet" ><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script><script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script><em>帶組的多選</em><br><select data-placeholder="你最喜歡的足球隊" style="width:350px;"class="chosen-select" 多個 tabindex="6"><option value=""></option><optgroup label="NFC 東"><option>達拉斯牛仔隊</option><option>紐約巨人隊</option><選項>費城老鷹隊</選項><選項>華盛頓紅皮隊</選項></optgroup><optgroup 標簽="NFC NORTH"><option>芝加哥熊隊</option><option>底特律雄獅隊</option><option>綠灣包裝工隊</option><option>明尼蘇達維京人隊</option></optgroup><optgroup label="NFC 南"><選項>亞特蘭大獵鷹隊</選項><選項>卡羅萊納黑豹隊</選項><option>新奧爾良圣徒隊</option><option>坦帕灣海盜隊</option></optgroup><optgroup label="NFC 西"><option>亞利桑那紅雀隊</option><選項>圣.路易斯·拉姆斯</option><option>舊金山 49 人隊</option><option>西雅圖海鷹隊</option></optgroup><optgroup label="亞足聯東區"><option>布法羅比爾</option><option>邁阿密海豚隊</option><option>新英格蘭愛國者隊</option><option>紐約噴氣機隊</option></optgroup><optgroup label="AFC NORTH"><option>巴爾的摩烏鴉隊</option><選項>辛辛那提孟加拉虎</選項><選項>克利夫蘭布朗隊</選項><選項>匹茲堡鋼人隊</選項></optgroup><optgroup label="亞足聯南區"><option>休斯頓德州人隊</option><選項>印第安納波利斯小馬隊</選項><選項>杰克遜維爾美洲虎隊</選項><option>田納西泰坦隊</option></optgroup><optgroup label="AFC 西區"><option>丹佛野馬隊</option><option>堪薩斯城酋長隊</option><option>奧克蘭突襲者隊</option><option>圣地亞哥閃電隊</option></optgroup></選擇></div>
我發現這樣做的唯一方法是一旦下拉菜單被隱藏 - 檢查輸入的值是否與第一個元素的值相同下拉菜單,如果是這樣 - 觸發該元素的 mouseup
(在下拉菜單內):
檢查實時樣本:
$(".chosen-select").chosen();$(".chosen-select").bind('chosen:hiding_dropdown', function(e, i) {searched_value = i.chosen.get_search_text();firstElementOnDropdown = i.chosen.search_results.find('li.active-result').first()if (firstElementOnDropdown.text().toLowerCase() == searched_value.toLowerCase()) {firstElementOnDropdown.trigger('mouseup');變量 t = i;設置超時(函數(){t.chosen.input_blur();}, 150);}});
<link rel="stylesheet" ><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script><script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script><em>帶組的多選</em><br><select data-placeholder="你最喜歡的足球隊" style="width:350px;"class="chosen-select" 多個 tabindex="6"><option value=""></option><optgroup label="NFC 東"><option>達拉斯牛仔隊</option><option>紐約巨人隊</option><選項>費城老鷹隊</選項><選項>華盛頓紅皮隊</選項></optgroup><optgroup 標簽="NFC NORTH"><option>芝加哥熊隊</option><option>底特律雄獅隊</option><option>綠灣包裝工隊</option><option>明尼蘇達維京人隊</option></optgroup><optgroup label="NFC 南"><選項>亞特蘭大獵鷹隊</選項><選項>卡羅萊納黑豹隊</選項><option>新奧爾良圣徒隊</option><option>坦帕灣海盜隊</option></optgroup><optgroup label="NFC 西"><option>亞利桑那紅雀隊</option><選項>圣.路易斯·拉姆斯</option><option>舊金山 49 人隊</option><option>西雅圖海鷹隊</option></optgroup><optgroup label="亞足聯東區"><option>布法羅比爾</option><option>邁阿密海豚隊</option><option>新英格蘭愛國者隊</option><option>紐約噴氣機隊</option></optgroup><optgroup label="AFC NORTH"><option>巴爾的摩烏鴉隊</option><選項>辛辛那提孟加拉虎</選項><選項>克利夫蘭布朗隊</選項><選項>匹茲堡鋼人隊</選項></optgroup><optgroup label="亞足聯南區"><option>休斯頓德州人隊</option><選項>印第安納波利斯小馬隊</選項><選項>杰克遜維爾美洲虎隊</選項><option>田納西泰坦隊</option></optgroup><optgroup label="AFC 西區"><option>丹佛野馬隊</option><option>堪薩斯城酋長隊</option><option>奧克蘭突襲者隊</option><option>圣地亞哥閃電隊</option></optgroup></選擇></div>
更新
添加 setTimeout
到 blur
輸入(導致菜單關閉),因為在 setTimeout
>選擇所以需要在顯示后隱藏它.
說明:在chosen
代碼中有一個setTimeout
函數來顯示菜單.我需要克服這種行為,所以我添加了一個新的 setTimeout
,但間隔更長.
setTimeout
有 2 個參數
- 運行函數
- 等待時間(以毫秒為單位).
該函數將在超時后運行.在我的示例中 - 該函數是調用 chosen
菜單的 input_blur
(以確保菜單被隱藏),并且我確保它被調用 afterem> 150 毫秒).
The below is my code my problem is i just want to implement like kendo i mean not with selection when user types the text and if we won't select the text it should be selected from drop down list as like in kendo In the below image you can see if you type medium and click mouse in sideways i mean without selecting with mouse it loads from drop down.
$(function(){
$(".chosen-select").chosen();
});
<link rel="stylesheet" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script>
<div>
<em>Multiple Select with Groups</em><br>
<select data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6">
<option value=""></option>
<optgroup label="NFC EAST">
<option>Dallas Cowboys</option>
<option>New York Giants</option>
<option>Philadelphia Eagles</option>
<option>Washington Redskins</option>
</optgroup>
<optgroup label="NFC NORTH">
<option>Chicago Bears</option>
<option>Detroit Lions</option>
<option>Green Bay Packers</option>
<option>Minnesota Vikings</option>
</optgroup>
<optgroup label="NFC SOUTH">
<option>Atlanta Falcons</option>
<option>Carolina Panthers</option>
<option>New Orleans Saints</option>
<option>Tampa Bay Buccaneers</option>
</optgroup>
<optgroup label="NFC WEST">
<option>Arizona Cardinals</option>
<option>St. Louis Rams</option>
<option>San Francisco 49ers</option>
<option>Seattle Seahawks</option>
</optgroup>
<optgroup label="AFC EAST">
<option>Buffalo Bills</option>
<option>Miami Dolphins</option>
<option>New England Patriots</option>
<option>New York Jets</option>
</optgroup>
<optgroup label="AFC NORTH">
<option>Baltimore Ravens</option>
<option>Cincinnati Bengals</option>
<option>Cleveland Browns</option>
<option>Pittsburgh Steelers</option>
</optgroup>
<optgroup label="AFC SOUTH">
<option>Houston Texans</option>
<option>Indianapolis Colts</option>
<option>Jacksonville Jaguars</option>
<option>Tennessee Titans</option>
</optgroup>
<optgroup label="AFC WEST">
<option>Denver Broncos</option>
<option>Kansas City Chiefs</option>
<option>Oakland Raiders</option>
<option>San Diego Chargers</option>
</optgroup>
</select>
</div>
The only way I found to do so is once the dropdown is hidden - check if the value of the input is the same as the value of the first element in the drop down, and if so - trigger the mouseup
for that element (inside the dropdown):
Check live sample:
$(".chosen-select").chosen();
$(".chosen-select").bind('chosen:hiding_dropdown', function(e, i) {
searched_value = i.chosen.get_search_text();
firstElementOnDropdown = i.chosen.search_results.find('li.active-result').first()
if (firstElementOnDropdown.text().toLowerCase() == searched_value.toLowerCase()) {
firstElementOnDropdown.trigger('mouseup');
var t = i;
setTimeout(function() {
t.chosen.input_blur();
}, 150);
}
});
<link rel="stylesheet" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script>
<div>
<em>Multiple Select with Groups</em><br>
<select data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6">
<option value=""></option>
<optgroup label="NFC EAST">
<option>Dallas Cowboys</option>
<option>New York Giants</option>
<option>Philadelphia Eagles</option>
<option>Washington Redskins</option>
</optgroup>
<optgroup label="NFC NORTH">
<option>Chicago Bears</option>
<option>Detroit Lions</option>
<option>Green Bay Packers</option>
<option>Minnesota Vikings</option>
</optgroup>
<optgroup label="NFC SOUTH">
<option>Atlanta Falcons</option>
<option>Carolina Panthers</option>
<option>New Orleans Saints</option>
<option>Tampa Bay Buccaneers</option>
</optgroup>
<optgroup label="NFC WEST">
<option>Arizona Cardinals</option>
<option>St. Louis Rams</option>
<option>San Francisco 49ers</option>
<option>Seattle Seahawks</option>
</optgroup>
<optgroup label="AFC EAST">
<option>Buffalo Bills</option>
<option>Miami Dolphins</option>
<option>New England Patriots</option>
<option>New York Jets</option>
</optgroup>
<optgroup label="AFC NORTH">
<option>Baltimore Ravens</option>
<option>Cincinnati Bengals</option>
<option>Cleveland Browns</option>
<option>Pittsburgh Steelers</option>
</optgroup>
<optgroup label="AFC SOUTH">
<option>Houston Texans</option>
<option>Indianapolis Colts</option>
<option>Jacksonville Jaguars</option>
<option>Tennessee Titans</option>
</optgroup>
<optgroup label="AFC WEST">
<option>Denver Broncos</option>
<option>Kansas City Chiefs</option>
<option>Oakland Raiders</option>
<option>San Diego Chargers</option>
</optgroup>
</select>
</div>
Update
Added setTimeout
to blur
the input (which cause the menu to close), since there is another setTimeout
inside the code of chosen
so needed to hide it after it's shown.
Explanation: Inside the
chosen
code there is is asetTimeout
function to show the menu. I needed to overcome this behavior so I added a newsetTimeout
, but with higher interval.
The setTimeout
gets 2 arguments
- Function to run
- Time to wait (in milliseconds).
The function will run after the timeout is up. In my sample - the function is call the input_blur
of the chosen
menu (to make sure the menu is hidden), and I made sure it's called after 150 milliseconds).
這篇關于如何在 jquery selected 插件中選擇沒有選擇的文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!