我有几个选择列表都具有相同的内容。我想要的是当一个选项被选中时,它将在其他选项中被禁用。
<div class="social-option">
<select>
<option selected="selected" value="facebook">facebook</option>
<option value="0"></option>
<option value="twitter">twitter</option><!-- If I select this -->
<option value="linkedin">linkedin</option>
<option value="instagram">instagram</option>
<option value="flickr">flickr</option>
</select>
</div>
<div class="social-option">
<select>
<option selected="selected" value="facebook">facebook</option>
<option value="0"></option>
<option value="twitter">twitter</option><!-- then this is disabled -->
<option value="linkedin">linkedin</option>
<option value="instagram">instagram</option>
<option value="flickr">flickr</option>
</select>
</div>
<div class="social-option">
<select>
<option selected="selected" value="facebook">facebook</option>
<option value="0"></option>
<option value="twitter">twitter</option><!-- and this also is disabled -->
<option value="linkedin">linkedin</option>
<option value="instagram">instagram</option>
<option value="flickr">flickr</option>
</select>
</div>
使用这个:
$('.social-option select').on('change', function () {
var newSelected = $('option:selected', this).val();
var newSelectedSiblings = $('.social-option select').find("option[value=" + newSelected + "]");
newSelectedSiblings.not(this).each(function() { //here is the .not selector that doesn't work
$(this).attr('disabled', 'disabled');
});
});
任何想法为什么?
另外,当我将选择器替换为以下内容时,为什么这条线不起作用this
:
var newSelectedSiblings = $('.social-option select').find("option[value=" + newSelected + "]");
像这样:
var newSelectedSiblings = $(this).find("option[value=" + newSelected + "]");
因为change
选择器是相同的:$('.social-option select').on('change'...
http://jsfiddle.net/ilyaD/4BBcZ/5/
更新
我找到了第一个问题的解决方案:
newSelectedSiblings.not(this)
我用了:
newSelectedSiblings.not('option:selected', this)