简体   繁体   English

如何将文本添加到选择框的某些选项?

[英]How to add text to some options of a select box?

In a form, I have a select box whose some options are pre selected by ajax according to a previous value entered. 在表单中,我有一个选择框,其一些选项由ajax根据输入的先前值预先选择。
I would like to add additional text to the options (only those which have been pre selected by ajax according to the previous value). 我想在选项中添加其他文本(仅那些由ajax根据先前的值预先选择的文本)。

for (i in data) {
$("#my_select_box_id").val(data[i][0])..attr('selected',true); //that works

//I would like to use something like that but it's not working
$("#my_select_box_id").text(data[i][1]);
}

I think I have to get the options pre selected,but that's not working too : 我想我必须预先选择选项,但是那也行不通:

$("#my_select_box_id option[value=data[i][0]).text(data[i][1]);
$("#my_select_box_id").html(data[i][1]);

To make it selected, you can use: 要选择它,可以使用:

$("#my_select_box_id").attr("selected", "selected");

*Edit, I'm actually not sure if it will select the option item when you use the above call, W3C specifies that it will on page-load... Can you report back? *编辑,实际上我不确定在使用上述调用时是否会选择选项,W3C指定它将在页面加载时显示...您可以报告吗?

-- -

To select the option elements you want, and to change the label, try: 要选择所需的选项元素并更改标签,请尝试:

$("#my_select_box_id option[value=" + data[i][0]+"]").html(data[i][1]);

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

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