【问题标题】:get selectedvalue of dropdownlist items added from javascript获取从 javascript 添加的下拉列表项的选定值
【发布时间】:2012-08-06 06:05:04
【问题描述】:

您好,我已从 javascript 向 asp:DropDownList 添加了一些项目。像这样

 if (document.getElementById("<%=gdview.ClientID %>")!=null)
    {
    var rows = document.getElementById("<%=gdview.ClientID %>").getElementsByTagName('tr');
    var cells = rows[1].getElementsByTagName('td');
  //alert(cells)
    i = 2;
    while (i < cells.length)
    {
        document.getElementById("<%=ddl_noofCols.ClientID %>").options[i] = new Option(i + 1, i);
        i++;
           }
    document.getElementById("<%=ddl_noofCols.ClientID %>").options[2].selected =true;
    alert(document.getElementById("<%=ddl_noofCols.ClientID %>").options[2].text);
    }

这里的gdview 是gridview。没有 gridview 的列被添加到下拉列表中 默认是options[2] 被选中。我无法使用返回 null 的ddl_noofCols.SelectedValue 获取选定项/选定值。

如何获得选定的值。

提前致谢。

【问题讨论】:

    标签: javascript asp.net vb.net


    【解决方案1】:

    你可能想要这样的东西:

    var table = document.getElementById("<%=gdview.ClientID %>");
    var select = document.getElementById("<%=ddl_noofCols.ClientID %>");
    var cells;
    
    if (table) {
        cells = table.rows[1].cells;
    
        for  (var i=2, iLen=cells.length; i<iLen; i++) {
            select.options[i] = new Option(i + 1, i);
        }
        select.options[2].selected = true;
        alert(select.options[2].text);
    
        // To get the current value of the select, 
        // use the value property:
        alert(select.value);
    }
    

    当前选定选项的值可作为 select.value 使用。如果所选选项没有 value 属性或属性,则返回 text 属性的值,但在 IE 8 及更低版本中,您必须使用类似以下内容:

    var value = select.value;
    
    if (!value) {
        value = select.options[select.selectedIndex].text;
    }
    

    【讨论】:

    • 我想在代码隐藏中获取选定的值,正如我提到的 ddl_noofCols.SelectedValue
    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多