admin管理员组

文章数量:1402849

        <script type="text/javascript">
            function ddl(id) {
      //          var id = document.getElementById('<%=DDLquestionType.ClientID%>').value;
                if (id == "Open") {
                    AmericanAnswer.style.display = 'none';
                    document.getElementById('Answer1_Btn').style.visibility = false;
                    
                }
                alert(id);
            }
        </script>
                <asp:DropDownList ID="DDLquestionType" CssClass="ddlQuestionType box"  runat="server" AutoPostBack="True" onchange="ddl(this)" >
                    <asp:ListItem Text="American" Value="American"></asp:ListItem>
                    <asp:ListItem Text="Open" Value="Open"></asp:ListItem>
                    <asp:ListItem Text="Yes/No" Value="YesNo"></asp:ListItem>
                    <asp:ListItem Text="Numerical" Value="Numerical"></asp:ListItem>
                </asp:DropDownList>

        <script type="text/javascript">
            function ddl(id) {
      //          var id = document.getElementById('<%=DDLquestionType.ClientID%>').value;
                if (id == "Open") {
                    AmericanAnswer.style.display = 'none';
                    document.getElementById('Answer1_Btn').style.visibility = false;
                    
                }
                alert(id);
            }
        </script>
                <asp:DropDownList ID="DDLquestionType" CssClass="ddlQuestionType box"  runat="server" AutoPostBack="True" onchange="ddl(this)" >
                    <asp:ListItem Text="American" Value="American"></asp:ListItem>
                    <asp:ListItem Text="Open" Value="Open"></asp:ListItem>
                    <asp:ListItem Text="Yes/No" Value="YesNo"></asp:ListItem>
                    <asp:ListItem Text="Numerical" Value="Numerical"></asp:ListItem>
                </asp:DropDownList>

I have this java script function:

Which I try to call on drop down list selection change

It doesn't work. I also tried onchange="javascript:ddl()" and function without parameters, ononchange="javascript:ddl(this);", ononchange="javascript:ddl(this.value);", and many others. I'm new to java script, any links with explanations also will be highly appreciated

Share Improve this question edited Aug 26, 2016 at 16:19 InnaS asked Aug 26, 2016 at 15:41 InnaSInnaS 601 gold badge1 silver badge8 bronze badges 4
  • Please don't post screenshots of your code. That's like going to a car repair shop with a photo of your car. Post your code. – Tomalak Commented Aug 26, 2016 at 15:48
  • @Tomalak Just copy-paste or there is better way to do it? – InnaS Commented Aug 26, 2016 at 15:54
  • When you edit your question, you should see a yellow box with tips how to use the editor. Among other things it describes how to post code. – Tomalak Commented Aug 26, 2016 at 16:00
  • The {} control of the editor formats the selected text as code, indenting it with 4 spaces on each line (you can indent it manually if you prefer). – Martin Parenteau Commented Aug 26, 2016 at 16:45
Add a ment  | 

2 Answers 2

Reset to default 3

you dosent need to pass this parameter or id, you just call the function and onchange() event call the function. in javascript use id of your dropdown list to get the value of dropdown list.

<script type="text/javascript">
function processchange(value)
{
  if(ddl.value=="American")
    // your code
}
</script>

<dropdownlist id="ddl" onchange="processchange()" >
  //enter your dropdown items
  
  </dropdownlist>

When you pass this as a parameter, you pass the DropDownList itself to the Javascript function. Therefore, you don't need to use the id to retrieve the control. You can get the value of the selected item with the value property, and you can access the items with the options array.

<asp:DropDownList runat="server" onchange="processChange(this);" ... >

function processChange(ddl) {
    if (ddl.value == 'Open') {
        var americanItem = ddl.options[0];
        ...
    }
}

Since you have set AutoPostBack="true" for the DropDownList, some of the changes that you make in your Javascript function may be lost after the postback. You can try with AutoPostBack="false" to see the difference.

本文标签: aspnetcall javascript function on asp dropdown list changeStack Overflow