i am using select2.
我正在使用select2。
Usage : if a value is "COUNTRY"
, i want to add a onchange event to the select2, else i want to remove the onchange event as shown below :
用法:如果值为“COUNTRY”,我想向select2添加onchange事件,否则我想删除onchange事件,如下所示:
var reportLevel = getReportLevel();
if (reportLevel != "COUNTRY") {
$("#selected_countries").on("change", function(e) {
prepareGlidModel(e);
});
} else {
$("#selected_countries").on("change", function(e) {
});
}
Issue : Not being able to remove the onchange event, even if the else block is called. Events are called based upon the reportLevel
value selected in a dropdown
.
问题:即使调用了else块,也无法删除onchange事件。根据在下拉列表中选择的reportLevel值调用事件。
3 个解决方案
#1
9
try following:
尝试以下:
var reportLevel = getReportLevel(),
// by default unbind/off change event
$select = $("#selected_countries").off("change");
// and if country then bind it
if (reportLevel == "COUNTRY") {
$select.on("change", function(e) {
alert("you selected :" + $(this).val());
});
}
Working example here: http://jsfiddle.net/JB89B/1/
这里的工作示例:http://jsfiddle.net/JB89B/1/
#2
3
i think on else case you want to reset the value to default for this you have to use
我认为在其他情况下你想要将值重置为默认值,你必须使用它
$("#selected_countries").val('default value')
and if u just want to prevent the default action of onchange event than on else you can write
如果你只是想阻止onchange事件的默认动作而不是其他你可以编写
e.preventDefault()
this will help
这会有所帮助
#3
1
You can use off to deatach event handlers from your elements as shown below:
您可以使用off从元素中取消事件处理程序,如下所示:
$("#selected_countries").off('change');
#1
9
try following:
尝试以下:
var reportLevel = getReportLevel(),
// by default unbind/off change event
$select = $("#selected_countries").off("change");
// and if country then bind it
if (reportLevel == "COUNTRY") {
$select.on("change", function(e) {
alert("you selected :" + $(this).val());
});
}
Working example here: http://jsfiddle.net/JB89B/1/
这里的工作示例:http://jsfiddle.net/JB89B/1/
#2
3
i think on else case you want to reset the value to default for this you have to use
我认为在其他情况下你想要将值重置为默认值,你必须使用它
$("#selected_countries").val('default value')
and if u just want to prevent the default action of onchange event than on else you can write
如果你只是想阻止onchange事件的默认动作而不是其他你可以编写
e.preventDefault()
this will help
这会有所帮助
#3
1
You can use off to deatach event handlers from your elements as shown below:
您可以使用off从元素中取消事件处理程序,如下所示:
$("#selected_countries").off('change');