首页 > 解决方案 > 需要在选择中添加一个附加选项,在编辑模式下,预先选择它并删除所需的类

问题描述

我有一个表格。它处于编辑状态(打开时将填充字段)或添加(未填充)。我有一个选择,其中包含从数据库中显示的选项。我需要添加一个附加选项供用户选择,该选项未包含在数据库中。如果表单处于编辑模式,并且选择了该选项(“未指定”),那么我需要用它填充选择并删除元素上所需的类。

我当前的代码将填充选择以供用户使用“未指定”进行选择,如果我处于编辑模式,它将正确填充值,但是我似乎无法删除所需的类(当需要的东西是填充,元素将变为绿色,使用此代码,它保持红色)。

关于我做错了什么的任何想法?谢谢!

<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>
            

标签: jquerycoldfusion

解决方案


添加.change();似乎会触发'select'添加选项的事件。

$('#sampling_event').val('Not Specified').change();

推荐阅读