最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Append values to asp.net dropdownlist with jQuery - Stack Overflow

programmeradmin1浏览0评论

I am trying to append a value with jQuery but with the ways I am trying the option doesn't want to append. This is what I am trying (for test purposes):

 $('#<%=btnSelectAgentAdd.ClientID %>').click(function () {              
                var myOptions = {
                    val1: 100,
                    val2: 'text2'
                };
                $('#<%=ddlAgentName.ClientID %>').append(new Option(myOptions.val2, myOptions.val1));             
             });

I also tried this:

 $('#<%=btnSelectAgentAdd.ClientID %>').click(function () {
                var mySelect = $('#<%=ddlAgentName.ClientID %>');
                var myOptions = {
                    val1: 100,
                    val2: 'text2'
                };
                $.each(myOptions, function(val, text) {
    $('#<%=ddlAgentName.ClientID %>').append( new Option(text,val) );
});           
             });

And this:

 $('#<%=btnSelectAgentAdd.ClientID %>').click(function () {
                var mySelect = $('#<%=ddlAgentName.ClientID %>');
                var myOptions = {
                    val1: 100,
                    val2: 'text2'
                };
                $.each(myOptions, function(val, text) {
    mySelect.append(
        $('<option></option>').val(val).html(text)
    );
             });

None of them are working. Debugging with Firebug shows that the error is happening on the last step when the values are to be appended to the dropdownlist.

I am trying to append a value with jQuery but with the ways I am trying the option doesn't want to append. This is what I am trying (for test purposes):

 $('#<%=btnSelectAgentAdd.ClientID %>').click(function () {              
                var myOptions = {
                    val1: 100,
                    val2: 'text2'
                };
                $('#<%=ddlAgentName.ClientID %>').append(new Option(myOptions.val2, myOptions.val1));             
             });

I also tried this:

 $('#<%=btnSelectAgentAdd.ClientID %>').click(function () {
                var mySelect = $('#<%=ddlAgentName.ClientID %>');
                var myOptions = {
                    val1: 100,
                    val2: 'text2'
                };
                $.each(myOptions, function(val, text) {
    $('#<%=ddlAgentName.ClientID %>').append( new Option(text,val) );
});           
             });

And this:

 $('#<%=btnSelectAgentAdd.ClientID %>').click(function () {
                var mySelect = $('#<%=ddlAgentName.ClientID %>');
                var myOptions = {
                    val1: 100,
                    val2: 'text2'
                };
                $.each(myOptions, function(val, text) {
    mySelect.append(
        $('<option></option>').val(val).html(text)
    );
             });

None of them are working. Debugging with Firebug shows that the error is happening on the last step when the values are to be appended to the dropdownlist.

Share Improve this question edited Nov 25, 2024 at 23:05 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jun 14, 2013 at 11:05 LazialeLaziale 8,24548 gold badges155 silver badges271 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Try like this:

$.each(myOptions, function (val, text) {
    mySelect.append($('<option />', {
        value: val,
        text: text
    }));
});

FIDDLE DEMO

var newOption = "<option value='"+"1"+"'>Some Text</option>"; 
$("#ddlCategory").append(newOption);

Reference How do I add an option to a dropdown list using jQuery?

try this

$('#<%=btnSelectAgentAdd.ClientID %>').click(function () {
                var mySelect = $('#<%=ddlAgentName.ClientID %>').append('<option value="100">yourtext</option><option value="100">yourtext</option>');

             });
发布评论

评论列表(0)

  1. 暂无评论