我希望捕获所选jquery的更改前事件,因此如果在所选的值中单击的值不是所需的值,则它什么也不做,并返回到以前所选的选项。有可能吗?
就像这样
$('.chzn-select').chosen({
'beforeChange': function(){
//if option clicked is Option-3
//do nothing
}
});发布于 2013-09-18 18:42:52
在 jQuery函数、change事件和params.selected参数的帮助下,你可以很容易地做到这一点。
$(".chzn-select").chosen().on("change", function (evt, params) {
if (params.selected === "Portugal") {
var previous = $(this).data("previous") || "";
$(this).val(previous).trigger("chosen:updated");
} else {
$(this).data("previous", params.selected);
}
});发布于 2013-09-18 18:03:26
像这样的东西
(function () {
var previous;
$(".chzn-select").focus(
function () {
// Store the current value on focus and on change
previous = $(this).val();
}).change(function() {
if($(this).val()=="Option-3"){// //if option clicked is Option-3
$(this).val(previous); // select the value prior to the change
$(this).trigger("chosen:updated"); // tell chosen to pick up the updated value
}
});
})();https://stackoverflow.com/questions/18868867
复制相似问题