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
});