admin管理员组文章数量:1422254
I am populating my dropdown through javascript using ASP.NET, and for that I have taken the help of one of the answers on this site. I used the following code:
<script type="text/javascript">
$(document).ready(function () {
alert("Hi");
var select = document.getElementById('<%=ddlItems%>');
var option = document.createElement("option");
option.value = "1";
option.innerHTML = "Option1";
select.appendChild(option);
});
</script>
It's not working. For checking whether the program flow is entering function, I have put an alert box. It's showing me the proper alert, which means its entering the function but below that code is not working. What may be the problem?
I am populating my dropdown through javascript using ASP.NET, and for that I have taken the help of one of the answers on this site. I used the following code:
<script type="text/javascript">
$(document).ready(function () {
alert("Hi");
var select = document.getElementById('<%=ddlItems%>');
var option = document.createElement("option");
option.value = "1";
option.innerHTML = "Option1";
select.appendChild(option);
});
</script>
It's not working. For checking whether the program flow is entering function, I have put an alert box. It's showing me the proper alert, which means its entering the function but below that code is not working. What may be the problem?
Share Improve this question edited Feb 13, 2013 at 4:05 David Robinson 78.7k16 gold badges172 silver badges189 bronze badges asked Feb 13, 2013 at 3:48 FreelancerFreelancer 9,0947 gold badges45 silver badges81 bronze badges 15- check the console for any errors – Hary Commented Feb 13, 2013 at 3:50
- I made a jsfiddle here: jsfiddle/BdhVv It seems to work fine for me, what does <%=ddItems%> turn into once the page is served? Make sure it's the same as the id of the element. – Tom Chandler Commented Feb 13, 2013 at 3:53
- @DON console is blank.nothing showing me on console – Freelancer Commented Feb 13, 2013 at 3:55
-
Respected User, are you sure
<%=ddlItems%>
is the id of an element on the page? Seems like a strange id for some reason. Please post your html. – Paul Hoenecke Commented Feb 13, 2013 at 3:55 -
are you getting
select
as anobject
? – Hary Commented Feb 13, 2013 at 3:56
4 Answers
Reset to default 5Use ddlItems.ClientID
<script type="text/javascript">
$(document).ready(function () {
alert("Hi");
var select = document.getElementById('<%=ddlItems.ClientID%>');
var option = document.createElement("option");
option.value = "1";
option.innerHTML = "Option1";
select.appendChild(option);
});
</script>
Try this way. set your id mode to static for dropdownlist
<asp:DropDownList ID="ddl" runat="server" ClientIDMode="Static"></asp:DropDownList>
and change your script to
var select = document.getElementById('ddl');
Hi you can add option as follows:
$('#ddlItems').append($('<option></option>').val("1").html("option1"));
Use clientID after the dropdownlist id..
<script type="text/javascript">
$(document).ready(function () {
alert("Hi");
var select = document.getElementById('<%=ddlItems.ClientID%>');
var option = document.createElement("option");
option.value = "1";
option.innerHTML = "Option1";
select.appendChild(option);
});
</script>
本文标签: jqueryjavascript dropdown fillingStack Overflow
版权声明:本文标题:jquery - javascript drop-down filling - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745363507a2655403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论