2017-06-12 236 views
0

我是jquery中的新成員。我正在使用多個選擇插件jQuery。我想用戶不能選擇超過3個選項。在這裏,我也禁用了全選option.Here是我的代碼: -如何在多個選擇插件中選擇限制數量的選項?

<select multiple id='testbox'> 
    <option value='1'>First Option</option> 
    <option value='2'>Second Option</option> 
    <option value='3'>Third Option</option> 
    <option value='4'>Fourth Option</option> 
    <option value='5'>Fifth Option</option> 
</select> 

jQuery代碼: -

$("select").multipleSelect({ 
    selectAll: false 
}); 

請幫助我。感謝提前:)

回答

1

編輯:

有與多選插件工作jsFiddle example

var limit = 3; 

var $multiSel = $("select").multipleSelect({ 
placeholder: "Here is the placeholder", 
width: 200, 
filter: true, 
selectAll: false, 
onClick: function(view) { 
    var $checkboxes = $multiSel.next().find("input[type='checkbox']").not(":checked"); 
    var selectedLen = $multiSel.multipleSelect('getSelects').length; 
    if (selectedLen >= limit) { 
     $checkboxes.prop("disabled", true); 
    } else { 
     $checkboxes.prop("disabled", false); 
    } 
} 
}); 
+0

不能與多個選擇插件 –

+0

此,如果不工作,工作您可以使用select選擇多個項目。點擊第一個元素,然後按Shift並點擊最後一個元素。 – Rajesh

+0

使用多選插件編輯,是更好的嗎? –

0

的選擇添加change事件,並檢查長度的選擇的選項

$("select").change(function() { 
 
     if($("select option:selected").length > 3) { 
 
      // you can disable rest of the things here 
 
     } 
 
    });

+0

我已經嘗試過這一點,但不能與多個工作選擇 –

+0

我更新了我的答案檢查現在 –

+0

這是多選不復選 –

0

你可以做

if($("select").multipleSelect("getSelects").length > 3) 
{ 
    alert('Not allowed'); 
} 
相關問題