我正在尝试填充属性quantity
等于零的表单选项值。
我的目标是在当前选项值中添加一条消息
我的html是:
<select class="valid">
<option disabled="disabled" selected="selected" value="">Select Bar</option>
<option value="1" quantity="99">value 1 </option>
<option value="2" quantity="0">value 2 </option>
</select>
到目前为止,我已经在jQuery中尝试了以下方法,但无法正常工作:
if($(this).attr('quantity') == '0') {
$(this).append('<span>message</span>');
}
最佳答案
您可以这样填充option
,
$(document).ready(function () {
$('.valid').on('change', function () {
if ($(this.options[this.selectedIndex]).attr('quantity') == 0) {
$(this.options[this.selectedIndex]).find('span').remove();
$(this.options[this.selectedIndex]).append('<span>Message</span>')
}
});
});
JSFIDDLE
关于javascript - 如何使用jQuery填充选项值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48011356/