开发者

how can i change selected value of drop list dynamically

开发者 https://www.devze.com 2023-04-10 06:13 出处:网络
i want to pick the value from text box and then change the value of dropdown list according to that value

i want to pick the value from text box and then change the value of dropdown list according to that value

<html>
    <head>
        <script>
            function change()
            {
                var value = document.getElementById('text').value;
                document.getElementById("Model").selectedvalue = value
            }
        </script>
    </head>
    <body>
       开发者_JAVA技巧 <asp:DropDownList ID="Model" AutoPostBack="false" runat="server" CssClass="styled">
            <asp:ListItem Value="None">None</asp:ListItem>
            <asp:ListItem Value="Enum">Enum</asp:ListItem>
            <asp:ListItem Value="Sum">Sum</asp:ListItem>
            <asp:ListItem Value="Multi">Multi</asp:ListItem>
            <asp:ListItem Value="Xaxis">Xaxis</asp:ListItem>
        </asp:DropDownList>
        <input id="text" type="text"/>
        <input type="button" onclick="change();"/>
    </body>
<html>


This may can help you http://www.java2s.com/Code/JavaScript/Form-Control/SelectinganOptionUsingJavaScript.htm


You can do this easily with jquery using following code

 function change()
    {
      $("#Model option[value='"+ $('#text').val()+"']").attr('selected', 'selected');    
     }


You need to change document.getElementById("Model").selectedvalue=valye to document.getElementById("<% = Model.ClientID %>").options.value=valye


your code will never find the Model, if you are using .NET 4 you can add

CLientIdMode="Static" 

to your drop down list

<asp:DropDownList ID="Model" AutoPostBack="false" runat="server" ClientIdMode="Static" CssClass="styled">
        <asp:ListItem Value="None">None</asp:ListItem>
        <asp:ListItem Value="Enum">Enum</asp:ListItem>
        <asp:ListItem Value="Sum">Sum</asp:ListItem>
        <asp:ListItem Value="Multi">Multi</asp:ListItem>
        <asp:ListItem Value="Xaxis">Xaxis</asp:ListItem>
    </asp:DropDownList>

Which means your

document.getElementById("Model").selectedvalue = value

will now find the control


you must write this :

document.getElementById("Model").value = value;

instead of your line :

document.getElementById("Model").selectedvalue = value;

More over if you are using master page @hungryMind answer is also correct, because in javascript there is .value not .selectedvalue.

0

精彩评论

暂无评论...
验证码 换一张
取 消