2

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. Every advice is welcome. Thanks, Laziale

4

3 回答 3

2

试试这样:

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

小提琴演示

于 2013-06-14T11:12:36.240 回答
1
var newOption = "<option value='"+"1"+"'>Some Text</option>"; 
$("#ddlCategory").append(newOption);

参考如何使用 jQuery 向下拉列表添加选项?

于 2013-06-14T11:09:52.660 回答
0

try this

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

             });
于 2013-06-14T13:23:13.923 回答