【问题标题】:jQuery combobox: standard script to catch selected value of combobox does not workjQuery组合框:捕获组合框选定值的标准脚本不起作用
【发布时间】:2011-06-15 03:14:51
【问题描述】:

我在 http://jqueryui.com/demos/autocomplete/combobox.html 使用 jqueryui 组合框示例

我添加了如下所示的脚本来捕获组合框的选定值:

<div id="selectedOpt">
</div>
<script>
$(document).ready(function() {
    $("#combobox").change(function() {
        var retval = $(this).val();
        $("#selectedOpt").html("retval=" + retval);
    });
});
</script>

但是,它并没有按预期工作:

  1. div selectedOpt 每次都不显示组合框的选定值 发生更改事件
  2. 如果选择“显示底层效果”(请尝试url above),标准下拉列表 出现。当试图改变 那个下拉列表,然后是 div selectedOpt 能够显示价值 正确。

目标是让div selectedOpt 显示组合框的选定选项。 请告知并解释为什么(1)不起作用而(2)起作用。

PS:所有必需的 js、css 均已正确包含。 感谢您的关注。

找到的解决方案: http://robertmarkbramprogrammer.blogspot.com/2010/09/event-handling-with-jquery-autocomplete.html

【问题讨论】:

  • 请使用答案发布您的解决方案,而不是更新问题。然后您可以将其标记为已接受。

标签: jquery-ui


【解决方案1】:

请更改您的脚本以匹配以下代码:

<script>
function test()
{ 
var retval = $("[id *=dropdown] :selected").val();
$("#selectedOpt").html("retval=" + retval);
}
</script>

然后像这样从服务器端调用这个脚本:

dropdown.Attributes.Add("onchange","javascript: return test();")

【讨论】:

    【解决方案2】:

    要在 div 中显示组合框的标签或值,您必须将函数作为选项包含在内。像这样的:

    $( ".selector" ).autocomplete({
     change: function(event, ui) {
      $("#selectedOpt").html("retval=" + ui.item.value);
     }
    });
    

    如果您想要一个标签,请使用 ui.item.label。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-08
      • 2013-08-15
      • 2011-10-17
      • 2011-05-23
      • 2013-10-27
      • 1970-01-01
      相关资源
      最近更新 更多