如何在 jquery selected 插件中选择没有选择的文本

How to choose the text without selection in jquery chosen plugin(如何在 jquery selected 插件中选择没有选择的文本)
本文介绍了如何在 jquery selected 插件中选择没有选择的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

下面是我的代码我的问题是我只想像剑道一样实现我的意思是当用户键入文本时不选择文本,如果我们不选择文本,应该从下拉列表中选择它,就像

$(function(){$(".chosen-select").chosen();});

<link rel="stylesheet" href="http://harvesthq.github.io/chosen/chosen.css"><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" href="http://harvesthq.github.io/chosen/chosen.css"><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>

更新

添加 setTimeoutblur 输入(导致菜单关闭),因为在 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" href="http://harvesthq.github.io/chosen/chosen.css">
<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" href="http://harvesthq.github.io/chosen/chosen.css">
<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 a setTimeout function to show the menu. I needed to overcome this behavior so I added a new setTimeout, 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 插件中选择没有选择的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)