基于此更改选项值's在jQuery中的当前值

Change option value based on it's current value in jQuery

本文关键字:jQuery 于此更 选项      更新时间:2024-01-06

我有以下HTML:

<select class="file_image_type" name="file_image_type">
        <option>Gallery</option>
        <option>Feature</option>
        <option>Thumbnail</option>
    </select>

这是动态生成的,并对每个项目重复。由于只能有一个Feature图像,每当一个新图像被选择为"Feature"时,我想重置任何标记为"Feature"的图像的选项值。

我试过这个jQuery,但它似乎没有响应:

current.find(".file_image_type option[value='Feature']").val("Gallery");

我可以使用以下功能将图像设置为功能:

current.find(".file_image_type").val("Feature");

"Gallery"不是val(),而是text()

人,人,你不需要直接在jQuery中选择option。我真不敢相信我看到了多少次这个错误。

$('.file_image_type').change(function() {  
    $('.file_image_type').not(this).each(function() {
        var $this = $(this);
        if ($this.val() === 'Feature') {
            $this.val('Gallery');
        }
    });
});​