Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable the multiple selection from the list box using jquery? or javascript?

I have a list box in my page..

<td><%=Html.ListBox("listServiceTypes", Model.ServiceTypeListAll, new { style = "width: 500px;height:200px;" })%>

I need to disabled selecting multiple items from the list box? I am doing something like selecting one item and click delete button my page its delting one item from list box.. but If I select multple Items its throwing an error message/.?

Can any body help me out how to deactive or disable multiple items from list box

like image 839
kumar Avatar asked Dec 09 '10 20:12

kumar


People also ask

How to disable multi select in jQuery?

multiselect('disable'); will disable the selector (stored in the variable $widget ). And by replacing disable with enable you can enable it. So just run the function with the correct disable/enable setting and you can do it based on any condition.

How to get multiple selected values and items from listbox in jQuery?

You can take multiple selected text by iterating loop as mentioned below. $('#f1'). click(function(){ var rr = []; $('. selectpicker :selected').


1 Answers

You could do that with the following jQuery:

$(function(){
  $("select[name='listServiceTypes']").removeAttr('multiple');
});

However, it would be much better to do it at the server side. Rather than using Html.ListBox, it would be better to use Html.DropDownList:

<%=Html.DropDownList("listServiceTypes", 
                Model.ServiceTypeListAll, 
                new { style = "width: 500px;height:200px", size=4 }); %>

This removes the need from having to do any jQuery/JavaScript to remove the multiple attribute as it produces pretty much the same HTML but without the multiple attribute. Having a value for size that is greater than 1 tells the browser to display it as a multi-line list box.

like image 125
Jonathon Bolster Avatar answered Oct 21 '22 05:10

Jonathon Bolster