jquery - 需要在选择中添加一个附加选项,在编辑模式下,预先选择它并删除所需的类
问题描述
我有一个表格。它处于编辑状态(打开时将填充字段)或添加(未填充)。我有一个选择,其中包含从数据库中显示的选项。我需要添加一个附加选项供用户选择,该选项未包含在数据库中。如果表单处于编辑模式,并且选择了该选项(“未指定”),那么我需要用它填充选择并删除元素上所需的类。
我当前的代码将填充选择以供用户使用“未指定”进行选择,如果我处于编辑模式,它将正确填充值,但是我似乎无法删除所需的类(当需要的东西是填充,元素将变为绿色,使用此代码,它保持红色)。
关于我做错了什么的任何想法?谢谢!
<cf_FormGroupTag elementType="select" elementName="sampling_event" elementId="sampling_event" mainLabel="Sampling Event:" mainLabelWidth="col-sm-4 col-md-3" elementDivClass="col-sm-8 col-md-9" inputClass="input-md required required-extend" query="SamplingEvent" optionDisplay="sampling_event" optionValue="sampling_event" selectedOptions="#sampling_event_value#" initialDisabledOption="yes" initialDisabledOptionDisplay="sampling event"/>
$('#sampling_event').append($('<option>', {value:'Not Specified', text:'Not Specified'}));
if (isNew === 'NO' && sampling_event_value === 'Not Specified') {
$('#sampling_event').val('Not Specified').removeClass('required required-extend');
}
Select from the page source. 'Not Specified is being show as the input value in my form, but it is not here as an option.
<div class="form-group" >
<label for="sampling_event" class="control-label col-sm-4 col-md-3">Sampling Event:</label>
<div class="col-sm-8 col-md-9">
<select name="sampling_event" id="sampling_event" class="input-md required required-extend">
<option value="" selected disabled>--Select sampling event--</option>
<option value="Event 1">Event 1</option>
<option value="Event 2">Event 2</option>
</select>
</div>
解决方案
添加.change();
似乎会触发'select'
添加选项的事件。
$('#sampling_event').val('Not Specified').change();
推荐阅读
- javascript - jsPDF-AutoTable 中的行跨度问题
- css - html类在css中不起作用?(尽管语法正确)
- swift - 如何在 iOS 12 中获得更高的 GPS 精度
- python - 如果熊猫末尾有“-”符号,则将值转换为负浮点数
- perl - Perl 中的“不是数组引用”
- metpy - 使用 Metpy 和 Pyart 绘制 Nexrad 2 级文件,坐标计算
- scheme - 如何将字符串转换为方案中的整数列表?
- c# - 如何使用组合框从 xml 文件中获取所需信息到列表框?
- javascript - 将动态生成的选中复选框的值传递给 ASP.NET MVC 中的方法
- python - networkX中无向图的参数传递闭包