简体   繁体   English

基于第一个字段值的自动完成jQuery两个字段需要在第二个字段中填充为自动完成

[英]Autocomplete jquery two fields based on the first field value needs to populate as autocomplete in second field

I have two text fields with id and name using jquery documentation I have succesfully achieved autocomplete for first text box now I am trying when the user type the value in first text field the relevant sub products needs to be shown second field as autocomplete. 我使用jquery文档有两个具有id和name的文本字段,我已经成功实现了第一个文本框的自动完成功能,现在我正在尝试在用户在第一个文本字段中键入值时,将相关子产品显示为第二个字段作为自动完成功能。

Here Is my HTML code for auto complete 这是我的自动完成的HTML代码

<input type="text" id="Product" name="Product"/>

<input type="text" id="subProduct" name="subProduct"/>

Jquery code for the current one 当前一个的jQuery代码

$(function() {
var availableItem = [
    "Fruits",
    "Vegetables",
    "Accesories"
];
var availableItem1 = [
    "Apple",
    "Strawberies",
    "Grapes"
];
 var availableItem2 = [
    "Potato",
    "LadiesFinger",
    "BitterGuard"
];
var availableItem3 = [
    "Headset",
    "Mouse",
    "Keyboard"
];  
  $("#Product").autocomplete({
    source:availableItem
  });
  $("#subProduct").autocomplete({
    source:availableItem
 });
});

Here is the fiddle Link 这是小提琴链接

Kindly suggest me how do this 请建议我怎么做

Bind function to your first input box as the code described. 按照所述代码将功能绑定到您的第一个输入框。 This will work 这会起作用

$("#Product").on('input change keyup autocompletechange',function(){
    if(availableItem.indexOf($("#Product").val()) != -1){
        $("#subProduct").autocomplete({
            source:eval('availableItem'+(availableItem.indexOf($("#Product").val())+1).toString())
        });
    }
});

See fiddle here 在这里看到小提琴

这是在StackoverFlow上已经给出的解决方案,它将显示如何在更改后附加值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM