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
2 Answers
Reset to default 3you 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
版权声明:本文标题:asp.net - call javascript function on asp dropdown list change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744321838a2600538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论