jQuery用户界面自动完成与项目和id [英] jQuery UI autocomplete with item and id

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

问题描述

我有以下脚本,用1维数组的作品。是否有可能得到这个带有2维数组工作?然后选择哪个项目,通过点击页面上的第二个按钮,应显示被选中任何项目的标识。

这是与1维数组的脚本:

 变量$ local_source =C ++,Java的,PHP,ColdFusion的,JavaScript的,ASP,红宝石];
$(#txtAllowSearch)。自动完成({
    来源:$ local_source
});

这是剧本的按钮检查ID,它是不完整的:

  $('#键')。点击(函数(){
    //警报($(#txtAllowSearch)someone_get_id_of_selected_item);
});


解决方案

您需要使用ui.item.label(文本),并ui.item.value(标识)的属性。

  $('#选择')。自动完成({
    来源:URL,
    选择:函数(事件,UI){
        $(#txtAllowSearch)VAL(ui.item.label)。 //显示选中的文本
        $(#txtAllowSearchID)VAL(ui.item.value)。 //选择ID保存为隐藏输入
    }
});$('#键')。点击(函数(){
    警报($(#txtAllowSearchID)VAL()); //从隐藏的输入获得的ID
});

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

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

 变量$ local_source = [0,C ++],[1,Java的],[2,PHP] [3的ColdFusion],
                     [4的javascript],[5,ASP],[6,红宝石]];

了解更多关于如何使用多维数组在这里工作:<一href=\"http://www.javascriptkit.com/javatutors/literal-notation2.shtml\">http://www.javascriptkit.com/javatutors/literal-notation2.shtml

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.

This is the script with the 1 dimensional array:

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

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);
});

解决方案

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
}); 

[Edit] 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"]];

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

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

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