Jquery val()无法工作,即动态添加选项

rah*_*hul 5 javascript jquery internet-explorer drop-down-menu

我正在通过jquery ajax方法为我的下拉列表生成选项,用db填充它.

$.ajax({
        type: "POST",
        url: pageUrl + '/FillAssignee',
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
                        for (var i = 0; i < response.d.length; i++) {
                            $('#SelectAssignee').append($("<option></option>").val(response.d[i]['Value']).html(response.d[i]['Text']));
                        }
                        if (response.d.length > 0) 
                        {
                          $('#SelectAssignee>option:eq(1)').attr('selected', true); 
                          alert($('#SelectAssignee').val()); //this line giving me correct value but value not showing in dropdown as selected in ie
                        }
                  }            
      });
Run Code Online (Sandbox Code Playgroud)

它的工作正常.唯一的问题是默认情况下没有选择第一个值.所以我使用了很多选项

1. $('#SelectAssignee').val();
2. $('#SelectAssignee option:first').attr('selected','selected');
3. $('#SelectAssignee option:first').prop('selected',true);
Run Code Online (Sandbox Code Playgroud)

如何让它工作,请帮助我.

提前致谢.

Bry*_*yan 0

您的逻辑是说您的目标是选择菜单中的第一个元素,但是,您要将新选项添加​​到选择输入的末尾。您需要将 .append 更改为 .prepend,或者在将选定属性设置为 true 时定位最后一个选项