Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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/1/visual-studio-2008/2.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
Jquery 无法触发文档就绪中下拉元素的更改事件_Jquery_Internet Explorer 8 - Fatal编程技术网

Jquery 无法触发文档就绪中下拉元素的更改事件

Jquery 无法触发文档就绪中下拉元素的更改事件,jquery,internet-explorer-8,Jquery,Internet Explorer 8,在$(document).ready()中,我正在设置要选择的特定下拉列表的第一个元素。我还需要触发下拉菜单上的更改功能,就像手动选择该选项一样。如果触发更改,将调用showTestsByPanel函数,并显示与所选下拉选项相关的适当数据 我正在执行以下操作,但这无助于触发更改功能: $(document).ready(function () { $("#lbpanel option:first-child").attr('selected', 'selected'); $('#

$(document).ready()
中,我正在设置要选择的特定下拉列表的第一个元素。我还需要触发下拉菜单上的更改功能,就像手动选择该选项一样。如果触发更改,将调用
showTestsByPanel
函数,并显示与所选下拉选项相关的适当数据

我正在执行以下操作,但这无助于触发更改功能:

$(document).ready(function () {
    $("#lbpanel option:first-child").attr('selected', 'selected');
    $('#lbpanel').trigger('change'); // <-- this is not triggering the change

    $('#lbpanel').change(function () {
        var $this = $(this);
        var parentId = $("#lbpanel").find("option:selected").val();
        $("#selectedTests tr").each(function () {
            showTestsByPanel(parentId, $(this));
        });
    });
});
$(文档).ready(函数(){
$(“#lbpanel option:first child”).attr('selected','selected');

$(“#lbpanel”).trigger('change');//您试图在创建处理程序之前触发
change
事件。这就是为什么没有效果

因此,在注册
change
事件处理程序后调用
change()

$(document).ready(function () {
    $("#lbpanel option:first-child").attr('selected', true);
    $('#lbpanel').change(function () {
        var $this = $(this);
        var parentId = $("#lbpanel").find("option:selected").val();
        $("#selectedTests tr").each(function () {
            showTestsByPanel(parentId, $(this));
        });
    }).change(); //added change here
});

您需要在触发器处理程序注册后触发更改事件,当事件被触发时,它将只调用那些已经注册的处理程序

$(document).ready(function () {
    $("#lbpanel option:first-child").attr('selected', 'selected');

    $('#lbpanel').change(function () {
        var $this = $(this);
        var parentId = $("#lbpanel").find("option:selected").val();
        $("#selectedTests tr").each(function () {
            showTestsByPanel(parentId, $(this));
        });
    }).trigger('change'); // need to trigger the event after the handler is added
});