javascript - 代码在结果中工作但在博客中不起作用

标签 javascript jquery mouseevent jquery-chosen

我正在使用jquery chosen实际上,我想实现如下图所示的结果,因为它在代码片段中显示的结果中运行良好。但是当我在我的 blog 中上传相同的代码时这是在博客中运行的,它不起作用。我所做的一切都是正确的。下面是我在堆栈片段结果中实现的图像,但不在我的博客中实现。

enter image description here

$(".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>
   
 
    
  </select>
</div>

我在我的博客中实现了下面的代码

<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="" />
    <optgroup label="NFC EAST">
      <option />Dallas Cowboys
      <option />New York Giants
      <option />Philadelphia Eagles
      <option />Washington Redskins
    </optgroup>



  </select>
</div>


<script type="text/javascript">
$(".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);
  }
});
</script>

当我打开博客时,我发现控制台中出现 2 个错误。下面是该图像

image

最佳答案

经过一些调试,我看到你的 if 语句:

if (firstElementOnDropdown.text().toLowerCase() == searched_value.toLowerCase())

返回 false。快速解决方法是:

if (firstElementOnDropdown.text().toLowerCase().trim() == searched_value.toLowerCase().trim())

因为有空格导致 if 子句出错。 如果不需要的话,使用 trim 函数删除字符串中的前导和尾随空格是一种很好的技术。

<script type="text/javascript">
$(".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().trim() == searched_value.toLowerCase().trim()) {
    firstElementOnDropdown.trigger('mouseup');
    var t = i;
    setTimeout(function() {
      t.chosen.input_blur();
    }, 150);
  }
});
</script>

关于javascript - 代码在结果中工作但在博客中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38771110/

相关文章:

javascript - AngularJS:如何从表单中的现有对象中获取更改后的键和值?

javascript - 可观察数字的 knockout 条件格式不起作用

javascript - 使用 Javascript 将单选按钮替换为选择元素

javascript - 设置媒体后无法在 jPlayer 中更改歌曲

java - 为什么当我进入或退出 JDialog 时,mouseExited 和 mouseEntered 方法同时运行?

javascript - 使用innerHtml渲染html表格时停止Jquery自动触发onclick事件

javascript - 单击任何按钮时关闭 div

c# - 检测鼠标的后退/前进按钮是否被按下

Java 按钮不起作用

javascript - 从 d3 中的系列创建系列