简体   繁体   English

如何在fcbkcomplete中选择值时获取ID? - jsfiddle补充道

[英]How to get ids when a value is selected in fcbkcomplete? - jsfiddle added

Please go through the below fiddle. 请仔细阅读以下小提琴。 I am trying to get the ids of the selected products in the fcbkcomplete box and show it as comma separated values in the textbox with id="interest" . 我试图在fcbkcomplete框中获取所选产品的ID,并在文本框中将其显示为逗号分隔值,其中id="interest" I wrote a function to achieve that but it didn't work. 我写了一个函数来实现它,但它没有用。 The function adds the id of the first value and not taking the ids of the other values which are added in the multiple selection box. 该函数添加第一个值的id,而不是取多个选择框中添加的其他值的id。

http://jsfiddle.net/balac/xDtrZ/1/ http://jsfiddle.net/balac/xDtrZ/1/

I have added json.txt . 我添加了json.txt it contains datas like this 它包含这样的数据

[{"key":"2", "value":"Canon Powershot "},{"key":"3", "value":"Fastrack Bag"},{"key":"4", "value":"Iphone 4 "},{"key":"5", "value":"Levis Jeans"},{"key":"7", "value":"Indig"},{"key":"8", "value":"Dashing Cars"},{"key":"9", "value":"dsdas"},{"key":"10", "value":"fsfs"}]

In the above json value key is the id which I want to display in the textbox as comma separated values. 在上面的json值中,key是我想在文本框中以逗号分隔值显示的id。 value is the value which will be coming in the dropdown for selection. value是将在下拉列表中选择的值。

while selecting the values in the drop down i want the corresponding key to get added in the textbox as comma separated values. 在下拉列表中选择值时,我希望相应的键以逗号分隔值的形式添加到文本框中。

The problem is that only the key of the first selected item is getting added in the textbox, no matter. 问题是,无论如何,只有第一个选定项的键才会添加到文本框中。

Hope am specific and said all in detail. 希望具体,并详细说明。 if anyone want any clarification please ask me i will explain more. 如果有人想要澄清,请问我,我会解释更多。

I think I found a simpler solution for you. 我想我找到了一个更简单的解决方案。 Keep in mind, due to the problems I mentioned in my comments I had to drastically simplify your fcbkcomplete code to get it working.. 请记住,由于我在评论中提到的问题,我必须大幅简化您的fcbkcomplete代码以使其正常工作..

$(document).ready(function() {
    $("#select3").fcbkcomplete({
        addontab: true,
        maxitems: 10,
        input_min_size: 0,
        height: 10,
        select_all_text: "select",
        onselect: clicker
    });
});

var clicker = function() {
    var selected = new Array();

    $('option', this).each(function() {
        selected.push($(this).val());
    });

    $('#interest').val(selected.join(', '));
};

See it in action here. 在这里看到它。

Note: I had to manually add <option>'s to the select list to get fcbkcomplete to actually work right. 注意:我必须手动将<option>'s添加到select列表中以使fcbkcomplete实际正常工作。 But nevertheless, my logic should work for you regardless. 但是,无论如何,我的逻辑应该适合你。

Also, fcbkcomplete apparently dynamically changes the <option>'s id to something like opt_vaQDzJU37ArScs818rc8HUwz9gm1wypP so I had to use the value instead. 此外, fcbkcomplete显然动态地将<option>'s id改为opt_vaQDzJU37ArScs818rc8HUwz9gm1wypP所以我不得不使用该值。 There are workarounds for that if you're dead set on using the id instead of the value. 如果你没有设置使用id而不是值,那么有一些解决方法。

To illustrate my point, modify the loop through each option like this: 为了说明我的观点,请修改每个选项的循环,如下所示:

$('option', this).each(function() {
    for (var i = 0; i < this.attributes.length; i++) {
        var pair = this.attributes[i].name + ': '
            + this.attributes[i].value;
        alert(pair);
    }
    selected.push($(this).val());
});

You will see how the attributes end up after fcbkcomplete runs. 您将看到在fcbkcomplete运行后属性如何结束。

Final Edit 最终编辑

After setting it up on localhost and using a JSON txt file, I was able to finally replicate the problem you were having. 在localhost上设置并使用JSON txt文件后,我终于可以复制您遇到的问题。 The thing is, the behavior totally changes when you use JSON instead of hard-coding the <option> s. 问题是,当您使用JSON而不是对<option>进行硬编码时,行为会完全改变。 Here is your working solution: 这是您的工作解决方案:

$(document).ready(function() {
    var clicker = function(e) {
        var selected = new Array();

        // using "this" here was out of context, use #select3
        $('option', $('#select3')).each(function() {
            selected.push(this.value);
        });

        $('#interest').val(selected.join(', '));
    };

    $("#select3").fcbkcomplete({
        json_url: "parseJSON.txt",
        addontab: true,
        maxitems: 10,
        input_min_size: 0,
        height: 10,
        select_all_text: "select",
        onselect: clicker
    }); 
});

Below link is example of getting value in fcbkcomplete on select. 下面的链接是在select中获取fcbkcomplete值的示例。 Same process you can do for id to. 您可以为id执行相同的过程。 https://github.com/emposha/FCBKcomplete/issues/110 example how to use : https://github.com/emposha/FCBKcomplete/issues/110示例如何使用:

`//auto complete jquery starts here
     $("#box").fcbkcomplete({
                    width: 250,
                    addontab: true,                   
                    maxitems: 1,
                    input_min_size: 0,
                    height: 10,
                    cache: true,
                    filter_case: true,
                    filter_hide: true,
                    filter_selected: true,
                    newel: true,
                    filter_case:false,
                    onselect: function(item)
                    {
                        getting_value_dealer(item._value, item._id);
                    }
                });
    //auto complete jquery ends here
`

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

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