仅隐藏选择了空值的选择框
我想只隐藏那些选择了空值的选择框。 我有以下HTML和jQuery
Jan Feb Mar Apr May Jun Jul Aug Jan Feb Mar Apr May Jun Jul Aug
为此,我使用以下代码
$('.date-month option').each(function() { if (this.selected) $(this).parent().hide(); else $(this).parent().show(); });
但它不起作用。
$(document).ready(function () { $('.date-month').each(function() { if ($(this).val()=='') $(this).hide(); }); });
您可以使用atrribute选择器查找具有所选值的元素:
$('.date-month option[selected="selected"]').parent().hide();
显示/隐藏选择:
$('.date-month').each(function() { if ($(this).find('option[selected="selected"]').length>0) { $(this).hide(); } else { $(this).show(); } });