带有项目和 id 的 jQuery UI 自动完成 [英] jQuery UI autocomplete with item and id

查看:27
本文介绍了带有项目和 id 的 jQuery UI 自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本可用于一维数组.是否有可能让它与二维数组一起工作?然后无论选择哪个项目,通过点击页面上的第二个按钮,都应该显示被选择的项目的 id.

I have the following script which works with a 1 dimensional array. Is it possible to get this to work with a 2 dimensional array? Then whichever item is selected, by clicking on a second button on the page, should display the id of whichever item is selected.

这是一维数组的脚本:

var $local_source = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"];
$("#txtAllowSearch").autocomplete({
    source: $local_source
});

这是按钮检查id的脚本,不完整:

This is the script for the button to check the id, which is incomplete:

$('#button').click(function() {
    // alert($("#txtAllowSearch").someone_get_id_of_selected_item);
});

推荐答案

您需要使用 ui.item.label(文本)和 ui.item.value(id)属性

You need to use the ui.item.label (the text) and ui.item.value (the id) properties

$('#selector').autocomplete({
    source: url,
    select: function (event, ui) {
        $("#txtAllowSearch").val(ui.item.label); // display the selected text
        $("#txtAllowSearchID").val(ui.item.value); // save selected id to hidden input
    }
});

$('#button').click(function() {
    alert($("#txtAllowSearchID").val()); // get the id from the hidden input
}); 

你还问了如何创建多维数组...

You also asked how to create the multi-dimensional array...

您应该能够像这样创建数组:

You should be able create the array like so:

var $local_source = [[0,"c++"], [1,"java"], [2,"php"], [3,"coldfusion"], 
                     [4,"javascript"], [5,"asp"], [6,"ruby"]];

在此处阅读有关如何使用多维数组的更多信息:http://www.javascriptkit.com/javatutors/literal-notation2.shtml

Read more about how to work with multi-dimensional arrays here: http://www.javascriptkit.com/javatutors/literal-notation2.shtml

这篇关于带有项目和 id 的 jQuery UI 自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆