Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling a Dropdown list item jquery not working

Tags:

jquery

asp.net

I want to disable the dropdown list item using jquery. The index value is -1 and the value is ------- Internal -------. I tried below syntaxes one by one(The solutions were the answers for similar questions). None of them are working. Iam using IE8.

var value="------- Internal -------";
$("[id*='ChildOrganizationDropDownList'] option[value=" + value + "]").prop('disabled','disabled'); 
$("#ChildOrganizationDropDownList option[value=" + value + "]").prop('disabled','disabled');    
$("[id*='ChildOrganizationDropDownList']").option('------- Internal -------').prop('disabled',true);    
$("id*='ChildOrganizationDropDownList' option[value='------- Internal -------']").prop('disabled','disabled');    
$("[id*='ChildOrganizationDropDownList']").option("[value*='------- Internal -------']").prop('disabled', true);
$("[id*='ChildOrganizationDropDownList']").attr("disabled",$(_this.CustomerNameDropDownList).find("option[value='------- Internal -------']"));

Designer : Aspx

<div class="selectionControls">
  <asp:CheckBox ID="chkSubcontracting" runat="server" Text="Subcontracting" Enabled="true" />
  <asp:HiddenField ID="SubcontractingHiddenField" runat="server" />
  <div class="subSelectionControl" id="AllowSubcotractingSelection" style="display: none;">
    <div class="subContractingControls">
      Parent BU:
      <span>
        <div class="subContractingControls">
          Supplier <em class="mandatoryIndicator">*</em>:
          <span>
            <asp:DropDownList ID="ChildOrganizationDropDownList" runat="server" Width="200px" />
            <asp:HiddenField ID="IxChildOrganizationHiddenField" runat="server" />
          </span>
        </div>
    </div>
  </div>

Aspx.cs

public Dictionary<int, string> ChildOrganizations
        {
            set
            {
                var result = value;
                result.Add(0, "Select Supplier");
                ChildOrganizationDropDownList.DataSource = result;
                ChildOrganizationDropDownList.DataTextField = "value";
                ChildOrganizationDropDownList.DataValueField = "key";
                ChildOrganizationDropDownList.DataBind();
                ChildOrganizationDropDownList.SelectedValue = "0";


            }
        }
like image 458
Manoj Nayak Avatar asked Apr 10 '15 11:04

Manoj Nayak


People also ask

How do I disable a specific item in a dropdown element?

The drop-down is used to create a list of items that need to select an element. We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element.

How do you enable disable a dropdown based on value in another dropdown in jQuery?

How do you enable disable a dropdown based on value in another dropdown in jQuery? val() == “ “) { $(“#ddl2”). prop(“disabled”, true); } else $(“#ddl2”). prop(“disabled”, false); }); }); The above code will disable the “ddl2” dropdown on select of a particular value in first dropdown.

How do you make a jQuery drop down list not editable?

JQuery code ready(function($) { var $select = $('#choose'), name = $select. prop('name'), $form = $select. parent('form'); //store the name in the data attribute $select. data('original-name', name); $('#toggle').

How can option disabled in jQuery?

Set the attribute disabled = "disabled" in the <option> element to make the custom select menu option disable.


1 Answers

I used each and it worked for me: https://jsfiddle.net/pz9curkb/1/

<select id="ChildOrganizationDropDownList">
    <option value="Default">Default</option>
    <option value="A">A</option>
    <option value="B">------- Internal -------</option>
    <option value="E">E</option>
</select>


 $("#ChildOrganizationDropDownList option").each(function(){   
     if($(this).text() === "------- Internal -------"){
        this.disabled  = true;
     }
});

Please note in this solution I used this instead of $(this), which means I used disabled option provided by javascript

like image 66
renakre Avatar answered Nov 06 '22 14:11

renakre