2012-07-23 40 views
0

电流值我有以下HTML:更改选项的值是在jQuery的

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

这是动态生成的,重复的每个项目。由于只能有一个特征图像,因此每当选择新图像作为“特征”时,我想重置任何标记为“特征”的选项值。

我试过这个jQuery,但它似乎并没有被回应:

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

我能够将图像作为功能就好用这个:

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

您的'option'元素没有'value'属性,所以您无法通过它选择 – MrOBrian 2012-07-23 17:41:45

+0

什么是javascript的上下文中的“current”? – 2012-07-23 17:43:37

+1

要使属性选择器起作用,元素必须具有该属性。 – 2012-07-23 17:44:02

回答

0

人们,你不需要直接在jQuery中选择option。我无法相信我有多少次看到这个错误。

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

对接受的答案Downvote没有评论?是什么赋予了? – Mathletics 2012-07-23 18:17:05

3

“画廊“不是val(),它是text()

+0

这是选项的文本,但除非实际更改内容,否则不需要选择该选项的选项。 – Mathletics 2012-07-23 17:57:04