【问题标题】:Need to add an additional option in a select, when in edit mode, preselect it and remove a required class需要在选择中添加一个附加选项,在编辑模式下,预先选择它并删除所需的类
【发布时间】:2020-07-10 14:07:07
【问题描述】:

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

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

关于我做错了什么有什么想法吗?谢谢!

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

【问题讨论】:

  • 最后一行选择了“未指定”选项,对吗?我在 jsfiddle 中尝试了一些测试,但在最新版本的 jquery 中没有发现您的方法有问题。你用的是什么版本的jquery?
  • 我意识到我遗漏了标签并添加了它。是的,在最后一行,如果页面是(不是 isNew),那么我需要“未指定”才能“选中”。版本 3.3.1 谢谢!
  • 你能展示一个从页面源输出的选择标签的例子吗?为了清楚起见,应该发生 3 件事...... 1)添加未指定选项以选择 2)选择未指定选项 3)从选择中删除类。我是否正确理解 1 和 2 目前正在正确发生,但 3 不是?
  • 你有&lt;cf_formGroupTag&gt;的任何信息
  • 谢谢丹!对不起,我很忙,现在才回来回应这个问题。是的,所以我查看了控制台,似乎即使选择了该值,它也没有“已选择”的属性。我试过 .attr('selected', true) 而不是删除仍然不起作用的类。

标签: jquery coldfusion


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    相关资源
    最近更新 更多