我在我的网站中使用多选下拉列表.这是正常工作,我们可以从该列表中选择多个选项.但我想从该列表中仅选择3个选项.是否可以设置限制? ?
我正在使用http://www.aleixcortadellas.com/main/2009/03/20/492/中的代码
使用jQuery和以下点击功能,它会有所帮助
$(document).ready(function()
{
$('#slectboxid option').click(function()
{
var items = $(this).parent().val();
if (items.length > 3) {
alert("You can only select 3 values at a time");
$(this).removeAttr("selected");
}
});
});
Run Code Online (Sandbox Code Playgroud)
编辑:使用.prop()代替.removeAttr(),.removeAttr()在最新的jQuery库中不推荐使用