如何在jQuery中强制调用.change函数
我有以下代码:如何在jQuery中强制调用.change函数,jquery,jquery-selectbox,Jquery,Jquery Selectbox,我有以下代码: $("select#sel_mode").change(function(){ if ($(this).val() == "N" || $(this).val() == "Y"){ $("table#options").hide(); } else { $("table#options").show(); if ($(this).val() == "C"){ $("tr#ip").hide()
$("select#sel_mode").change(function(){
if ($(this).val() == "N" || $(this).val() == "Y"){
$("table#options").hide();
} else {
$("table#options").show();
if ($(this).val() == "C"){
$("tr#ip").hide();
} else {
$("tr#ip").show();
}
}
});
当我在selectbox中手动进行更改时,它可以完美地工作
使用ajax,我从服务器获取值,并根据需要将selectbox放在某个位置的值,如:
$('#sel_mode').val(json_answer.mode);
但它不会显示/隐藏必要的元素,我需要再次添加类似的代码:
if (json_answer.mode == "N" || json_answer.mode == "Y"){
$("table#options").hide();
} else {
$("table#options").show();
if (json_answer.mode == "C"){
$("tr#ip").hide();
} else {
$("tr#ip").show();
}
}
是否可能以某种方式强制调用已定义的方法。更改选择框?所以我需要避免重复代码。请给我一些建议。谢谢。您可以在不带参数的情况下调用jQuery change函数来强制运行该函数
$("select#sel_mode").change();
这是
$(“选择选择模式”)的快捷方式。触发器('change')代码>