且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将课程添加到下拉选项

更新时间:2023-11-29 23:43:04

更新
更简单,更快捷的解决方案:

update
even simpler and faster solution:

$("#shopperlanguage option[value='ja_JP']").addClass("japimg");

(您在这里不需要 find ,因为您只需使用正确的选择器就可以过滤所有内容)

( you don't need find here, cause you can filter it all just with the right selector )

旧答案

忘记其他的javascript代码行.只需使用它来查找具有特定值的选项:

forget your other lines of javascript-code. simply use this to find options with a specific value:

 $("#shopperlanguage").find("option[value='ja_JP']").addClass("japimg");

编辑:正在运行 JSFiddle

加号::根据需要使用 option -值不是一个好习惯.它使下拉列表的样式取决于该值,在您的情况下,这可能是您所需要的.在其他情况下,您可能希望将值与样式分开.请参阅我的第二个示例以了解替代解决方案.


第二个示例
使用 data 属性 html :

plus: it isn't good practice to use the option-value for your needs. it makes the style of your dropdown depending on the value, which in your case could accidently be what you want. in other cases you might want to separate the values from your style. see my 2nd example for an alternative solution.


2nd example
using a data attribute, html:

<select class="input" id="shopperlanguage" name="shopperlanguage">
    <option value="en">English (International)</option>
    <option selected="" value="ja_JP" data-red="1">日本語</option>
    <option value="en" data-red="1">English (International)</option>
    <option selected="" value="ja_JP">日本語</option>
</select>

javascript 部分:

 $("#shopperlanguage").find("option[data-red=1]").addClass("japimg");

和:用于数据属性示例的JSFiddle