Javascript 页面加载后立即以编程方式触发jquery下拉列表更改事件
我有两个dropdownlist,如果两个都被更改,它将触发一个事件。 这意味着,对索引的任何更改都将触发一个基于dropdownlist当前值的json请求,并将它们附加到我的表中 我的问题是,一旦页面加载,是否有任何方法可以预先选择它们的索引并同时触发事件?我计划根据当前学期和学年设置它们 以下是我的jquery代码:Javascript 页面加载后立即以编程方式触发jquery下拉列表更改事件,javascript,jquery,html,Javascript,Jquery,Html,我有两个dropdownlist,如果两个都被更改,它将触发一个事件。 这意味着,对索引的任何更改都将触发一个基于dropdownlist当前值的json请求,并将它们附加到我的表中 我的问题是,一旦页面加载,是否有任何方法可以预先选择它们的索引并同时触发事件?我计划根据当前学期和学年设置它们 以下是我的jquery代码: $schoolyear = $('select#schoolyear'); $schoolterm = $('select#schoolterm') $tbl = $('#c
$schoolyear = $('select#schoolyear');
$schoolterm = $('select#schoolterm')
$tbl = $('#classview');
$schoolyear.change(function () {
getCL();
});
$schoolterm.change(function () {
getCL();
});
function getCL() {
$.getJSON('@Url.Action("getClassList","Enrollment")', { term: $schoolterm.val(), year: $schoolyear.val() }, function (e) {
$tbl.find('tbody').remove();
if (e.length > 0) {
$(e).each(function (index, e) {
$tbl.append('<tr><td>' + e.subj + '</td><td>' + e.days + '</td><td>' + e.cstart + '</td><td>' + e.cend + '</td><td>' + e.professor + '</td><td>' + e.units + '</td><td>' + e.status +
'</td>' + '<td>' + '<form action="/Enrollment/dropClass" method="post">' + '<input type="hidden" name="test" value="'+e.id+'"/>' +
'<a href="#"> Delete </a>' + '</form></td></tr>')
});
}
else {
$tbl.append('<tr><td colspan="8">No match found</td></tr>');
}
//compute t
});
}
尝试在脚本底部添加getCL
$schoolyear = $('select#schoolyear');
$schoolterm = $('select#schoolterm')
$tbl = $('#classview');
$schoolyear.change(function () {
getCL();
});
$schoolterm.change(function () {
getCL();
});
function getCL() {
$.getJSON('@Url.Action("getClassList","Enrollment")', { term: $schoolterm.val(), year: $schoolyear.val() }, function (e) {
$tbl.find('tbody').remove();
if (e.length > 0) {
$(e).each(function (index, e) {
$tbl.append('<tr><td>' + e.subj + '</td><td>' + e.days + '</td><td>' + e.cstart + '</td><td>' + e.cend + '</td><td>' + e.professor + '</td><td>' + e.units + '</td><td>' + e.status +
'</td>' + '<td>' + '<form action="/Enrollment/dropClass" method="post">' + '<input type="hidden" name="test" value="'+e.id+'"/>' +
'<a href="#"> Delete </a>' + '</form></td></tr>')
});
}
else {
$tbl.append('<tr><td colspan="8">No match found</td></tr>');
}
//compute t
});
}
getCL(); //this will run once when page is loaded.
可以在设置值时触发其中一个的更改
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var term = Math.ceil(month/4);// needs verification on how term is set
$schoolyear
.change(getCL)
.val(year);//set year value
$schoolterm
.change(getCL)
// trigger change after setting value, will call getCL()
.val(term).change();
你能不能在页面加载的时候运行getCL?嗯,我明白了,这可能行得通。getCL获取dropdownlist的值,既然getCL以该值为基础,如何设置dropdownlist的值?$schoolyear.val'someValue'ahhhhh好的,谢谢,或者如果默认值始终相同,只需将selected=selected添加到默认选项中,然后调用$schoolyear.change on load
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var term = Math.ceil(month/4);// needs verification on how term is set
$schoolyear
.change(getCL)
.val(year);//set year value
$schoolterm
.change(getCL)
// trigger change after setting value, will call getCL()
.val(term).change();