简体   繁体   English

从数组填充下拉列表

[英]Populate Drop Down from Array

I am trying to populate an array list string so that I can then populate my drop downs. 我正在尝试填充数组列表字符串,以便随后可以填充下拉列表。

I pass it an array string, and setup my array list like this: 我向它传递一个数组字符串,并像这样设置我的数组列表:

 var thirdParties = [{'7-Eleven', '114'},{'A Mart All Sports', '41'},{'A.J Coory Dental', '140'}];

I am then trying to populate my drop downs like this: 然后,我试图像这样填充下拉列表:

if (id == 1) // Payment
        {
            //alert('Payment');
            $('#SourceEntityId').empty();
            $.each(accounts, function (val, text) {
                $('#SourceEntityId').append($('<option></option>').val(val).html(text));
            });

            $('#DestinationEntityId').empty();
            $.each(thirdParties, function (val, text) {
                $('#DestinationEntityId').append($('<option></option>').val(val).html(text));
            });
        }


        if (id == 2) // deposit
        {
            //alert('Deposit');
            $('#SourceEntityId').empty();
            $.each(thirdParties, function (val, text) {
                $('#SourceEntityId').append($('<option></option>').val(val).html(text));
            });

            $('#DestinationEntityId').empty();
            $.each(accounts, function (val, text) {
                $('#DestinationEntityId').append($('<option></option>').val(val).html(text));
            });
        }

However, no items arrive in the drop down. 但是,下拉列表中没有项目到达。 Is what I am doing, possible? 我在做什么,可能吗?

Your object inside array was wrong. 您数组中的对象错误。

DEMO 演示

var thirdParties = [{
    text: '7-Eleven',
    value: '114'
}, {
    text: 'A Mart All Sports',
    value: '41'
}, {
    text: 'A.J Coory Dental',
    value: '140'
}];

$('#SourceEntityId').empty();

$.each(thirdParties, function () {
    console.log(this);
    var thirdParty = this;
    $('#SourceEntityId').append($('<option></option>')
         .val(thirdParty.value).html(thirdParty.text));
});

OR Another Way - DEMO 或另一种方式- 演示

var thirdParties = [['7-Eleven','114'],['A Mart All Sports','41'],['A.J Coory Dental','140']];

$('#SourceEntityId').empty();

$.each(thirdParties, function () {

    var thirdParty = this;
    console.log(thirdParty[1]);
    $('#SourceEntityId').append($('<option></option>')
        .val(thirdParty[1]).html(thirdParty[0]));
});

You have two problems here: 您在这里有两个问题:

1.You array isn't valid: 1.您的数组无效:

[{'7-Eleven', '114'},{'A Mart All Sports', '41'},{'A.J Coory Dental', '140'}];

What you have here is an array that holds invalid objects. 您这里拥有的是一个包含无效对象的数组。
If you wanted an array of arrays it should of looked like this: 如果您想要一个数组数组,它​​应该看起来像这样:

[['7-Eleven', '114'],['A Mart All Sports', '41'],['A.J Coory Dental', '140']];

If you wanted a valid array of objects then you need to provide key properties.Something like this: 如果您想要有效的对象数组,则需要提供键属性,如下所示:

[{text:'7-Eleven', value:'114'},{text:'A Mart All Sports', value:'41'},{text:'A.J Coory Dental', value:'140'}];

2.jquery each's callback doesnt hold val\\text but index\\object : 2.jQuery每个的回调不包含 val\\text index\\object

$.each(accounts, function (idx, obj) {});

and therefore if we use the valid array then: 因此,如果我们使用有效数组,则:

var arr = [{text:'7-Eleven', value:'114'},{text:'A Mart All Sports', value:'41'},{text:'A.J Coory Dental', value:'140'}];

$.each(arr, function (idx, obj) {
      $('#SourceEntityId').append($('<option></option>').val(obj.value).html(obj.text));
});

Here's a Fiddle . 这是小提琴

When you do .html () that emptys the element and inputs the variable. 当您执行.html()时,该元素将清空并输入变量。 You would want to use .append()... 您可能想使用.append()...

Basically everytime it runs through that loop it resets the container. 基本上,每次通过该循环运行时,都会重置容器。

Another thing is you can dynamically create a unordered list and append the list its that way and just have data tags to hold the values. 另一件事是,您可以动态创建一个无序列表,然后以这种方式附加该列表,并且仅使用数据标签来保存值。 It will give you more room to play with style and animation. 它将为您提供更多空间来播放样式和动画。

var list_container = $("<ul/>").addClass ('list');



$('whatever container you decide this is going into').append(list_container);

$.each(thirdparties,  function (index){
     var li = $("<li/>").addClass('classname').attr('data-value',thirdparties[index].value);
     $('.list').append(li);
     $('li[data-value="'+thirdparties[index].value+'"]).html(thirdparties [index].text);
});

Thats just something really quick I daw this from my phone. 那只是我快速从手机上摘下的东西。 I would plug it in jsfiddle and give it a try. 我将其插入jsfiddle并尝试一下。

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

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