简体   繁体   English

下拉列表选项值和名称与数组javascript

[英]Dropdown list option value and name with array javascript

I'm having trouble with array in javascript. 我在javascript中遇到数组问题。 Is it possible to insert name and value in the same array 是否可以在同一数组中插入名称和值

here is my code for array 这是我的数组代码

press=new Array('T shirt black - $23','Sweater black - $34','Hoodie shirt black - $87');

and here is what I want to be the output 这是我想成为的输出

<option value="23">T shirt black - $23</option>

how can I put the "23" in the array? 如何将“ 23”放入数组?

Here is the full code. 这是完整的代码。 Sorry if I can't explain it 对不起,我无法解释

http://jsfiddle.net/V64hb/1/ http://jsfiddle.net/V64hb/1/

http://pastebin.com/zyiQGHTS http://pastebin.com/zyiQGHTS

You can use an array of objects: 您可以使用对象数组:

press = [
    { desc: 'T shirt black',
      price: 23
    },
    { desc: 'Sweater black'
      price: 34
    },
    { desc: 'Hoodie shirt black'
      price: 87
    }
];

You can then access them with press[i].desc and press[i].price . 然后,您可以使用press[i].descpress[i].price来访问它们。

press.forEach(function(item) {
    $('#item').append($('<option/>', {
        value: item.price,
        text: item.desc+' - $'+item.price
    }));
});

Here's the full rewrite of your fiddle. 这是您的小提琴的完整改写。 I also got rid of eval() , by putting all the different arrays into an object keyed off the category value. 通过将所有不同的数组放入以类别值为键的对象中,我还摆脱了eval()

$(document).ready(function () {
    dropdowns = {
        web: [
            {desc: "1 Custom Web Page", price: 39},
            {desc: "5 Custom Web Pages", price: 190},
            {desc: "10 Custom Web Pages", price: 375},
            {desc: "20 Custom Web Pages", price: 710}
        ],
        art: [{desc: '300-word Article x 1', price: 7.00},
              {desc: '300-word Article x 5', price: 32.00},
              {desc: '400-word Article x 1', price: 8.00},
              {desc: '400-word Article x 5', price: 37.00},
              {desc: '500-word Article x 1', price: 9.00},
              {desc: '500-word Article x 5', price: 40.00},
              {desc: '700-word Article x 1', price: 12.00},
              {desc: '700-word Article x 5', price: 57.00},
              {desc: '1000-word Article x 1', price: 15.00},
              {desc: '1000-word Article x 5', price: 70.00}],
        blog: [{desc: '300-word Custom Blog x 1', price: 10},
               {desc: '400-word Custom Blog x 1', price: 12},
               {desc: '500-word Custom Blog x 1', price: 14},
               {desc: '300-word Custom Blog x 5', price: 47},
               {desc: '400-word Custom Blog x 5', price: 55},
               {desc: '500-word Custom Blog x 5', price: 63}
              ],
        press: [{desc: '300-word Custom Press Release', price: 27},
                {desc: '400-word Custom Press Release', price: 37},
                {desc: '500-word Custom Press Release', price: 47}
               ]
    }

    populateSelect();

    $(function () {
        $('#cat').change(populateSelect);
    });

    function populateSelect() {
        var cat = $('#cat').val();
        $('#item').empty();

        dropdowns[cat].forEach(function(item) {
            $('#item').append($('<option/>', {
                value: item.price,
                text: item.desc+' = $'+item.price
            }));
        });
    }

});

DEMO DEMO

The simplest way is for me, spliteach entry uing entry.split('-')[1]; 对我来说,最简单的方法是使用entry.split('-')[1]; . After that the entry will be $23 . 此后,条目将为$23 Trim it (using entry.trim() , and get the substring from the second character. This will remove the $. 修剪(使用entry.trim() ,然后从第二个字符中获取子字符串。这将删除$。

You could potentially use an array of objects (PHP uses associative arrays for this; these are also referred to as hash maps, and by many other names in many other different languages): 您可能使用一个对象数组(为此,PHP使用了关联数组;这些也称为哈希映射,在许多其他不同语言中也被称为其他名称):

Here's the jsfiddle : 这是jsfiddle

var clothing = [

    {
         type: 't-shirt',
         color: 'black',
         price: 23
    },

    {
         type: 'sweater',
         color: 'black',
         price: 34
    }

]

var i, len = clothing.length;

for (i=0; i<len; i++) {
    var obj = clothing[i];

    console.log('object at index ' + i + ' of clothing array - ');

    for (key in obj) {
        console.log(key + ':' + obj[key]);
    }
}

OUTPUT: OUTPUT:

object at index 0 of clothing array -
type:t-shirt
color:black
price:23

object at index 1 of clothing array -
type:sweater
color:black
price:34

you can use split function if array string in same as array and only you want output. 如果数组字符串与数组相同并且仅需要输出,则可以使用拆分功能。

clothing=new Array('T shirt black - $23','Sweater black - $34','Hoodie shirt black - $87');


for (i=0; i<clothing.length; i++) {
    var a = clothing[i].split(" - ");
    $('#item').append('<option value = "'+a[1]+'">'+a[0]+'</option>');
}

http://jsfiddle.net/nilesh026/6qg8C/1/ http://jsfiddle.net/nilesh026/6qg8C/1/

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

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