简体   繁体   English

将Java脚本更改为更新面板

[英]Change Javascript to Update Panel

I have JavaScript that works excellently but I need to change it to Update Panel and I can't get it to still work properly. 我有运行良好的JavaScript,但是我需要将其更改为“更新面板”,但我无法使其正常工作。

What I want it to do is if it selects 1 in the first dropdown, pick c in the second dropdown. 我要执行的操作是,如果它在第一个下拉列表中选择1,然后在第二个下拉列表中选择c。

I am using asp.net vb. 我正在使用asp.net vb。 I appreciate all the help! 我感谢所有的帮助!

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function NumbersDropDownList_OnChange() {
            var numbersDropDownList = document.getElementById("numbersDropDownList");
            if (numbersDropDownList.options[numbersDropDownList.selectedIndex].text=="1") {
                document.getElementById("lettersDropDownList").selectedIndex = 2;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="numbersDropDownList" onchange="NumbersDropDownList_OnChange()" runat="server">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="lettersDropDownList" runat="server">
            <asp:ListItem>a</asp:ListItem>
            <asp:ListItem>b</asp:ListItem>
            <asp:ListItem>c</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Hope this helps 希望这可以帮助

    <asp:UpdatePanel runat="server" ID="pnllettersDropDownList" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DropDownList ID="numbersDropDownList" runat="server" AutoPostBack="true" >
        <asp:ListItem Value="1" >1</asp:ListItem>
        <asp:ListItem Value="2" >2</asp:ListItem>
        <asp:ListItem Value="3" >3</asp:ListItem>
    </asp:DropDownList>
        <asp:DropDownList ID="lettersDropDownList" runat="server">
        <asp:ListItem Value="1" >a</asp:ListItem>
        <asp:ListItem Value="2" >b</asp:ListItem>
        <asp:ListItem Value="3" >c</asp:ListItem>
    </asp:DropDownList>        
    </ContentTemplate>
    </asp:UpdatePanel>

Protected Sub numbersDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles numbersDropDownList.SelectedIndexChanged
        if numbersDropDownList.selectedvalue=2 then
          lettersDropDownList.selectedvalue=3
        end if
End Sub

It looks like you are calling your controls incorrectly from the javascript. 您似乎从javascript错误地调用了控件。 You are just "getting lucky" that your current script works, simply because your lists are in the base level of the form. 您只是因为自己的列表位于表单的基础级别而“幸运”地知道当前的脚本有效。

Correct your script as below: 如下更正您的脚本:

<script type="text/javascript">
    function NumbersDropDownList_OnChange() {
        var numbersDropDownList = document.getElementById('<%= numbersDropDownList.ClientID %>');
        if (numbersDropDownList.options[numbersDropDownList.selectedIndex].text=="1") {
            document.getElementById('<%= lettersDropDownList.ClientID %>').selectedIndex = 2;
        }
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM