Jquery从dropdownlist中删除项目
jQuery(document).ready(function () { $lstAccountType = $('select[id*="account_type"]'); $lstAccountType.change(function () { $(this).remove('option[text="select one"]') }); });
我想在点击时删除下拉列表中的第一个元素。 有没有人对此有任何指示?
你应该只能使用:
$lstAccountType.change(function () { $lstAccountType.find("option[value='"+$lstAccountType.val()+"']").remove(); });
我没有对此进行测试,但是如果它有任何好处,请告诉我。
编辑
如果您只想在每次尝试时删除第一个选项:
$lstAccountType.change(function () { if ($lstAccountType.find("option:first").attr("value") == $lstAccountType.val()) { $lstAccountType.find("option[value='"+$lstAccountType.val()+"']").remove(); } });
它需要一些整理,但希望这可能会有所帮助。
编辑
如果您想要删除第一个选项,只需执行以下操作:
var first_option_removed = false; $lstAccountType.change(function () { if (!first_option_removed) { if ($lstAccountType.find("option:first").attr("value") == $lstAccountType.val()) { $lstAccountType.find("option[value='"+$lstAccountType.val()+"']").remove(); first_option_removed = true; } } });
$(this).remove('option:first')
应该这样做,如果你确定你总是想删除第一个。
编辑:
$(this).remove('option:contains(select one)');
应该在文字上做。
或者,如果您想要更容易阅读的内容,请记住您可能想要绑定onFocus
因为您希望在单击选择后立即删除第一个列表项 。
$('#mySelectId').bind('focus' function(){ var listItems = $('#mySelect li'); if(listItems.length > 0){ $(listItems[0]).remove(); } });