我正在使用我的网站中的多选下拉列表。这是正常工作,我们可以从该列表中选择多个选项。但我想从该列表中只选择3个选项。是否可以设置限制那??是否可以在多选下拉列表中设置限制?
我使用从http://www.aleixcortadellas.com/main/2009/03/20/492/
我正在使用我的网站中的多选下拉列表。这是正常工作,我们可以从该列表中选择多个选项。但我想从该列表中只选择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");
}
});
});
编辑:使用.prop()
代替.removeAttr()
,.removeAttr()
中弃用最新的jQuery库
在formsubmit.php
变线3以下行:
if (isset($options) && count($options) == 3) {
然后修改线26以下:
echo 'Please select exactly 3 options';
我编辑的代码,它完美 – nu6A
我会去的jQuery的解决方案也为更好的用户体验,但不要忘记在PHP中验证(如果javascript被禁用) –
是的,服务器端验证是必须的。我同意你mr.claarman – nu6A