删除下拉列表中的重复项

Delete duplicates in dropdown list

本文关键字:下拉列表 删除      更新时间:2023-09-26

我已经硬编码并添加了项目到dropdownlist ie teamsize,如1,2,3。

当我加载这个下拉列表进行编辑/更新时,我会得到类似的重复值

1

1

2

3

4…如何消除这些重复值?

请在下面找到代码

         <select name="anesthesia" id="selectAnesthesiaVal" style="width:25%;" class="required safe" AppendDataBoundItems = "false">
          <option value="<?php echo isset($event)?$event->proc_anesthesia_type:"" ;?>"><?php echo isset($event)?$event->proc_anesthesia_type:"" ;?></option><option value="General">General</option>
          <option value="Mac">Mac</option>
          <option value="Spinal/Epidural">Spinal/Epidural</option>
          <option value="Regional">Regional</option>
          <option value="Local">Local</option>
          <option value="Other">Other</option>
        </select>

如果您不想使用jQuery,那么我建议将所有可能的值放入一个数组中,并在PHP中循环使用。如果该值存在,则只放置一次。

此外,如果你想使用jQuery,而PHP在你的情况下是不可能的,那么请告诉我,我会发布一些jQuery。

更新

这就行了。我已经清楚地列出了评论,以逐步解释正在发生的事情。希望这能有所帮助。

请注意,在PHP中这样做会更有效率

// Set the present object
var present = {};
$('#selectAnesthesiaVal option').each(function(){
    // Get the text of the current option
    var text = $(this).text();
    // Test if the text is already present in the object
    if(present[text]){
        // If it is then remove it
        $(this).remove();
    }else{
        // Otherwise, place it in the object
        present[text] = true;
    }
});

我怀疑

<option value="<?php echo isset($event)?$event->proc_anesthesia_type:"" ;?>"><?php echo isset($event)?$event->proc_anesthesia_type:"" ;?></option>

添加与从选择控件中选择的选项相对应的选项,即在第一次加载时,您会看到编码列表,因为此行返回空选项,但当您选择实际选项时,第一个选项会填充选择值

在这种情况下,你需要做两件事1删除此行2为硬编码选项的每行添加条件,并将其设置为根据$event->proc_anesthesia_type的值进行选择由于第二个坦克,你最终会得到6个几乎相同的条件语句,将selected='selected'放入每个选项

因此,为了使整个代码看起来很漂亮,我建议不要硬编码选项,而是将值添加到列表或更好的字典中,并在循环

中检查此条件