我正在尝试播放一个选择,这样当用户从下拉列表中选择一个产品时,它就会被禁用,因为所采取的操作将不允许在每个产品上超过一次。这是可行的,但如果您单击下拉列表,它仍然被选中但已禁用,我希望自动选择第一个未禁用的可用<option>,以便能够选择它并结束故事;)有任何线索吗?
$('#product_name').find('option:not([disabled]):first-of-type');
$('#product_name').find('option:not([disabled]):first-child');
$('#product_name').find('option:nth-child(1):not([disabled])');当然,我也玩过:not(:disabled),但是从我关于这个主题的测试时间来看,CSS选择器对于没有禁用的元素很好,但是当第二个parameter === requisite开始运行时,这将是“选择第一个与规则匹配的元素”,所有的东西都会变暗,只是不能工作。
发布于 2016-10-29 06:44:59
使用:first选择器:
第一个伪类等价于:eq( 0 ).它也可以写成:lt( 1 )。虽然这只匹配单个元素,但第一个子元素可以匹配多个元素:每个父元素一个。
$('select option:not([disabled]):first').addClass('highlight');.highlight {
background: yellow;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select>
<option disabled>Disabled One</option>
<option disabled>Disabled One</option>
<option>Enabled One</option>
<option>Enabled One</option>
<option selected>Enabled One</option>
<option disabled>Disabled One</option>
<option disabled>Disabled One</option>
<option>Enabled One</option>
</select>
https://stackoverflow.com/questions/40314808
复制相似问题