简体   繁体   English

在下拉菜单中设置选项值

[英]set option value in dropdown

I am attempting to set an option in a dropdown/select. 我试图在下拉列表/选择中设置一个选项。 I have done this before, but for some reason I am getting an error and I am not sure why. 我之前已经做过,但是由于某种原因,我遇到了错误,并且不确定为什么。

Code

var _loc = {
        ALL_BUYS: [{ Text: "All Buys", Value: "AAA"}],
        NETWORK_BREAK: [{ Text: "Network Break", Value: "NTN"}],
        SYNDICATED_BREAK: [{ Text: "Syndicated Break", Value: "STA"}],
        LOCAL_BREAK: [{ Text: "Local Break", Value: "LTA"}],
        NATIONAL_LOCAL: [{ Text: "National/Local", Value: "LTN"}],
        LOCAL_LOCAL: [{ Text: "Local/Local", Value: "LTL"}]//,
    };

lstOptions = $("<select></select>");

$.each(_loc, function (i, item)
    {
        $("<option></option>")
            .appendTo(lstOptions)
            .html(item.Text)
            .val(item.Value) // receive error: Object  has no method 'val'
        });

You're not grabbing the values from the object correctly. 您没有从对象中正确获取值。

The format is like so: 格式如下:

{key1:[{0, 1}], key2:[{0, 1}], key3:[{0, 1}]}

When you use $.each , it selects each one in this state: 当您使用$.each ,它将在此状态下选择每个:

[{0, 1}]

On the first iteration, it would select the value associated with key1, on the second, key2, etc. 在第一个迭代中,它将选择与key1关联的值,在第二个迭代中,选择key2等等。

Since you have arrays nested in each key's value, you need to specify that it locate the first array in the matched value: 由于您在每个键的值中嵌套了数组,因此需要指定它在匹配值中找到第一个数组:

var _loc = {
    ALL_BUYS: [{ Text: "All Buys", Value: "AAA"}],
    NETWORK_BREAK: [{ Text: "Network Break", Value: "NTN"}],
    SYNDICATED_BREAK: [{ Text: "Syndicated Break", Value: "STA"}],
    LOCAL_BREAK: [{ Text: "Local Break", Value: "LTA"}],
    NATIONAL_LOCAL: [{ Text: "National/Local", Value: "LTN"}],
    LOCAL_LOCAL: [{ Text: "Local/Local", Value: "LTL"}]
};

lstOptions = $("select");

$.each(_loc, function(i, item){
    $("<option></option>")
        .appendTo(lstOptions)
        .html(item[0].Text)
        .attr("value", item[0].Value);
});​

Demo . 演示

Note the item[0] . 注意item[0] What this does is select the array with the index 0 in the value that it grabbed. 这样做是在其获取的值中选择索引为0的数组。 Since there is only one, that single array's index is 0 , so it's selecting that specific one. 由于只有一个,因此单个数组的索引为0 ,因此它将选择该特定数组。 Then you find and set its text and value. 然后找到并设置其文本和值。

Also note that I changed val() to attr() , because option elements don't support val() (you could, however, use it on a select element to set the option whose value is equal to the one specified in this function to make it selected). 还要注意,我将val()更改为attr() ,因为option元素不支持val() (但是,您可以在select元素上使用它来设置其值等于此函数中指定值的选项将其选中)。

TIP: You could also get rid of the literal array indicators around each nested object and use the original method. 提示:您还可以摆脱每个嵌套对象周围的文字数组指示符,并使用原始方法。 Demo . 演示

<option> elements do not support val() . <option>元素不支持val() I think you want to use attr() 我想你想使用attr()

.attr("value", item.Value)

.val() is used to get the value of form elements. .val()用于获取表单元素的值。 It is not a short-cut to setting the value attribute of an element. 设置元素的value属性不是捷径。 To do this, you can use: 为此,您可以使用:

.attr("value", item.Value)

Figured it out. 弄清楚了。 I want my items in the loop to be in the following format 我希望循环中的项目采用以下格式

Item: "All Buys"
Value: "AAA"

Instead they were in this format 相反,它们采用这种格式

[0]Option
  Item: "All Buys"
  Value: "AAA"

If I change my loop to this it works 如果我将循环改为

$.each(_loc, function (i, item)
{
     $("<option></option>")
           .appendTo(lstOptions)
           .html(item[0].Text)
           .val(item[0].Value);
});

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

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