Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery val()在IE中不适用于动态添加的选项_Javascript_Internet Explorer_Jquery_Drop Down Menu - Fatal编程技术网

Javascript jQuery val()在IE中不适用于动态添加的选项

Javascript jQuery val()在IE中不适用于动态添加的选项,javascript,internet-explorer,jquery,drop-down-menu,Javascript,Internet Explorer,Jquery,Drop Down Menu,我用jqueryajax方法为下拉列表生成选项,用db填充 $.ajax({ type: "POST", url: pageUrl + '/FillAssignee', data: {}, contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) {

我用jqueryajax方法为下拉列表生成选项,用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
                        }
                  }            
      });

如何使其工作?

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

尝试将其设置为属性而不是属性

$('#SelectAssignee').append($("<option />", { value: response.d[i]['Value'], html: response.d[i]['Text'] }));

你能试试下面的解决方案吗

在for循环之后,只需添加以下内容

if (response.d.length > 0) {
   $('#SelectAssignee>option:eq(1)').attr('selected', true);
}

我最好的猜测是这应该行得通。虽然,这看起来像是一个肮脏的黑客行为,但有一次我不得不求助于它,它奏效了

setTimeout( function() {
    $('#SelectAssignee option:first').prop('selected', true);
    $('#SelectAssignee').trigger('change');
}, 1000 );

在for循环之后,尝试将此代码块放入成功回调中。

在for循环之后,尝试以下条件

if (response.d.length > 0) { $('#SelectAssignee>option:eq(0)').attr('selected', 'selected');}

试试这样的

 var selObj = document.getElementById('SelectAssignee');
 selObj.selectedIndex = 0;

尝试将该值显式设置为第一个选项的值

$('#SelectAssignee').val(response.d[0]['Value']);

在设置所选参数后,尝试添加.trigger“change”property@Abhilash谢谢您的回复,但对我来说不起作用。您的ajax成功回调中是否有此循环?@techie_28是的,我正在我的jquery ajax方法的成功回调中应用此循环,通常用于设置选项显示文本,我一直使用.text而不是.html方法。可能没什么区别,但是值得一试。当您创建选项时,请发送文本。您为$'SelectAssignment>option:eq1获得的值是多少。valit会提醒我正确的值,但不会将其设置为selected。请告诉我您正在使用哪个版本的IE。当前默认情况下会选择哪个值?这是最后一个附加值吗?这个问题与这篇文章有关,但这篇文章中给出的答案对我也不起作用。@rahul,我能看看这个问题吗?可能像小提琴一样。它是从db填充的,所以不可能为+1制作小提琴。众所周知,IE更喜欢操纵options属性,而不是创建节点并添加节点,因此这很有可能奏效:
$('#SelectAssignee').val(response.d[0]['Value']);