JavaScript的:添加新的后选附加属性() [英] JavaScript: add extra attribute after new Option()

查看:165
本文介绍了JavaScript的:添加新的后选附加属性()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块code的动态地将选项取决于其他一些条件的选择字段。它看起来有点像这样的:

I have a piece of code that dynamically adds options to a select field depending on some other criteria. It looks somewhat like this:

if (chosen == "a_value") {
  selbox.options[selbox.options.length] = new Option('text_1','value_1');
  selbox.options[selbox.options.length] = new Option('text_2','value_2');
  selbox.options[selbox.options.length] = new Option('text_3','value_3');
}

我需要的是一个额外的属性添加到包含特定值的新选项。在这种情况下,该属性将被称为折扣和值将是一个整数。后来我读的属性值,并根据选项字段中的值进行处理。

What I need is to add an extra attribute to that new option that contains a specific value. In this case the attribute will be called "discount" and the value will be an integer. Later on I'll read the attribute values and process them based on the value in the Option field.

因此​​,一个选项看起来像这样,一旦脚本准备好;

So an option will look like this, once the script is ready;

<option value="value_1" discount="integer">text_1</option>

有道理?

现在我怎么能做到这一点,而无需使用JS框架。这是code,我需要的只是这一小部分,这样一个框架将是矫枉过正这个项目。

Now how can I do this without the use of JS frameworks. It's just this small part of code that I need, so a framework would be overkill for this project.

干杯! : - )

推荐答案

您可以这样做。



var o1 = new Option("key","value");
selbox.options[selbox.options.length] = o1;
o1.setAttribute("key","value");

这篇关于JavaScript的:添加新的后选附加属性()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆