2012-05-02 47 views

回答

7

使用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库

+0

我编辑的代码,它完美 – nu6A

+0

我会去的jQuery的解决方案也为更好的用户体验,但不要忘记在PHP中验证(如果javascript被禁用) –

+0

是的,服务器端验证是必须的。我同意你mr.claarman – nu6A

3

formsubmit.php变线3以下行:

if (isset($options) && count($options) == 3) { 

然后修改线26以下:

echo 'Please select exactly 3 options'; 
相关问题