根据jQuery中的当前值更改选项值 [英] Change option value based on it's current value in jQuery

查看:124
本文介绍了根据jQuery中的当前值更改选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML:

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

这是动态生成的,并针对每个项目重复进行.由于只能有一个特征图像,因此每当将新图像选为特征"时,我都想重置任何标记为特征"的选项值.

This is generated dynamically and is repeated for each item. Since there can only be one Feature image, I want to reset the option value of any labeled as "Feature" whenever a new image is selected as being a "Feature".

我已经尝试过使用此jQuery,但它似乎没有响应:

I've tried this jQuery but it doesn't seem to be responding:

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

我可以使用以下方法将图像设置为特征":

I'm able to set the image as Feature just fine using this:

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

推荐答案

人们,人们,您无需直接在jQuery中选择option.我不敢相信我有多少次看到这个错误.

People, people, you do not need to select the options directly in jQuery. I can't believe how many times I see this mistake.

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

这篇关于根据jQuery中的当前值更改选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆