jquery - 将事件绑定(bind)到选定的 select

标签 jquery onchange jquery-chosen jquery-events

我有一个选择框的代码,该选择框由选择的 jQuery 插件显示。我想要实现的是从已选择的当前项目中获取值:

<div>
  <select id="schedule_event" name="schedule[event]" style="display: none; " class="chzn-done"><option value="Game" selected="selected">Game</option>
    <option value="Event">Event</option>
    <option value="Training">Training</option>
  </select>
  <div id="schedule_event_chzn" class="chzn-container chzn-container-single chzn-container-active" style="width: 854px; ">
    <a href="javascript:void(0)" class="chzn-single chzn-single-with-drop" tabindex="-1">
      <span>Game</span>
      <div></div>
    </a>
    <div class="chzn-drop" style="width: 849px; top: 27px; left: 0px; ">
      <div class="chzn-search">
        <input type="text" autocomplete="off" style="width: 814px; " tabindex="0">
    </div>
    <ul class="chzn-results">
      <li id="schedule_event_chzn_o_0" class="active-result result-selected highlighted" style="">Game</li>
      <li id="schedule_event_chzn_o_1" class="active-result" style="">Event</li>
      <li id="schedule_event_chzn_o_2" class="active-result" style="">Training</li>
    </ul>
  </div>
</div>
</div>

我现在用来查找当前值的 JavaScript 是:

$("#schedule_event").chosen().change( function() {
    alert(+ $(this).text());
    $('#' + $(this).val()).show();
});

但是,我不断得到“NaN”,即没有值。

最佳答案

代码:

$(".chzn-select").chosen().change(function() {
    alert(+$(this).val());
    //$('#' + $(this).val()).show();
});

演示:

工作演示:http://jsfiddle.net/MM7wh/

编辑(2015 年 7 月 6 日)

cdns 现已更改。

工作演示:http://jsfiddle.net/uk82dm5L/

请使用$(this).val()而不是.text() .

关于jquery - 将事件绑定(bind)到选定的 select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11279898/

相关文章:

javascript - 在 html 文件中加载 Chosen.jquery.js 文件不起作用

javascript - 如何从 Chosen JS 中删除搜索框?

jQuery 验证插件和 Bootstrap 4

javascript - 禁用并重置或清空所有输入

javascript - Backbone.js "on change"事件未正确触发

javascript - 为什么这个函数 setFormData({ ...formData, [e.target.name] : e. target.value}); 中包含这些括号?

javascript - jQuery jGrowl 不工作

javascript - eBay 和 Bootstrap : collapse menu in javascript

javascript - 如何防止文本框失去焦点后提交按钮提交

javascript - Chosen JS - 将optgroup添加到所选项目