How to populate a drop down with values from JavaScript?

百般思念 提交于 2019-12-05 02:19:09

问题


I added a button to the ribbon toolbar button in my extension to the Tridion CMS. On click of the button a popup page will display with two drop downs. By changing the values in the first drop down control I should populate the values for second drop down control. In my case I am using ASP drop down list control. For the time being I will hard code the values to be populated to the second drop down in java script. For this requirement I am using the following code but I am not able to populate the value (Not Identifying the tag).

Java script code:

ABC.WCMS.RTFExtension.Popups.ButtonPopup.prototype._populate = function () {    
    var selectedValue = $('#functionalcomponent').value;//First dropdown selected value
    var dropdownId = $("#Dd");//Second Dropdown Control

        switch (selectedValue) {

            case "Home Ware":
                dropdownId.append($("<option> </option>").val("Select Sub-Category").html(""));
                dropdownId.append($("<option> </option>").val("Air-Conditioners/Coolers").html("Air-Conditioners/Coolers"));              
                break;
            case "Education":
                dropdownId.append($("<option> </option>").val("Select Sub-Category").html(""));
                dropdownId.append($("<option> </option>").val("Colleges").html("Colleges"));
                break;
            default:
                dropdownId.append($("<option> </option>").val("Select Sub-Category").html(""));
                dropdownId.append($("<option> </option>").val("No Value").html("No Value"));

        }
    return true;
}

ASPX Controls:

<%--Dropdown1--%>
<asp:DropDownList ID="functionalcomponent" runat="server"></asp:DropDownList>
<%--Dropdown2--%>
<asp:DropDownList ID="Dd" runat="server"></asp:DropDownList>

How can I populate the values for second drop down from external JavaScript file?


回答1:


Rather than adding values on demand, you may use the approach below:

  1. Add all the items beforehand to the DOM.
  2. Hide the required items using jQuery logic.

    You may refer the following post for a hint Hide options in a select list using jQuery

Please take a look at jQuery disable SELECT options based on Radio selected (Need support for all browsers) also




回答2:


I recently worked on a GUI extension where we populated a drop down based on the value of another drop down. When I populate using javascript I have the following code:

$j(c.SystemDropDown).append("<option value=\"" + value + "\">" + value + "</option>");

As you can see my example appends the whole <option> tag, where as yours is specified using the .val(), perhaps you could try this way?

The version I have is working great :)



来源:https://stackoverflow.com/questions/10943567/how-to-populate-a-drop-down-with-values-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!