select元素应该返回2个值

时间:2023-01-31 22:46:43

I have a select box with options in it. The value of the options gets the id of the item that is displayed in each option.
I do have an other value that should be linked on this option. Is there an easy way to do this?

我有一个选项框,里面有选项。选项的值获取每个选项中显示的项的ID。我确实有一个应该在此选项上链接的其他值。是否有捷径可寻?

<select id="category" name="data[cat]">
<option value="12" label="0">A</option>
<option value="7" label="0">B</option>
</select>

I tried to do this with entering the second value into a label attribute but then the problem is that I don't know the correct way to get the label value of the selected option. This is written in jQuery.

我尝试将第二个值输入标签属性,但问题是我不知道获取所选选项的标签值的正确方法。这是用jQuery编写的。

To get the selected option I use:

要获得所选的选项,我使用:

$("#category").livequery('change', function () {            
      var catId = ($(this)[0].value); 
});

This works great, but I just don't know how to get the label value of the selected option. Or maybe a better way to solve this problem.

这很好用,但我不知道如何获取所选选项的标签值。或者也许是解决这个问题的更好方法。

2 个解决方案

#1


The label attribute is used by some browsers to display the content of the option, so your list box might display two 0 options.

某些浏览器使用label属性来显示选项的内容,因此列表框可能会显示两个0选项。

A better solution might be to do the following:

更好的解决方案可能是执行以下操作:

<select id="category" name="data[cat]">
    <option value="12,0">A</option>
    <option value="7,0">B</option>
</select>

and split the value in the event handler, e.g.

并在事件处理程序中拆分值,例如

$("#category").livequery('change', function () {            
    var values = ($(this)[0].value).split(",");
    var catId = values[0];
    var otherId = values[1];
});

#2


One of the way is to set the value of option as an JSON string like

其中一种方法是将option的值设置为JSON字符串

<select id="category" name="data[cat]">
    <option value="{value1:10, value2:20}" label="0">A</option>
    <option value="{value1:30, value2:40}" label="0">B</option>
</select>

And then parse the value as JSON object. Once you get the JSON object then you can extract individual value from the same. In this way you can add as many values as you like.

然后将值解析为JSON对象。获得JSON对象后,您可以从中提取单个值。通过这种方式,您可以添加任意数量的值。

#1


The label attribute is used by some browsers to display the content of the option, so your list box might display two 0 options.

某些浏览器使用label属性来显示选项的内容,因此列表框可能会显示两个0选项。

A better solution might be to do the following:

更好的解决方案可能是执行以下操作:

<select id="category" name="data[cat]">
    <option value="12,0">A</option>
    <option value="7,0">B</option>
</select>

and split the value in the event handler, e.g.

并在事件处理程序中拆分值,例如

$("#category").livequery('change', function () {            
    var values = ($(this)[0].value).split(",");
    var catId = values[0];
    var otherId = values[1];
});

#2


One of the way is to set the value of option as an JSON string like

其中一种方法是将option的值设置为JSON字符串

<select id="category" name="data[cat]">
    <option value="{value1:10, value2:20}" label="0">A</option>
    <option value="{value1:30, value2:40}" label="0">B</option>
</select>

And then parse the value as JSON object. Once you get the JSON object then you can extract individual value from the same. In this way you can add as many values as you like.

然后将值解析为JSON对象。获得JSON对象后,您可以从中提取单个值。通过这种方式,您可以添加任意数量的值。