jQuery-在下拉菜单的确认对话框上取消更改事件 [英] jQuery - cancel change event on confirmation dialog for a Dropdown
问题描述
我有一个下拉菜单,我有jQuery change
函数.
I have a dropdown and I have the jQuery change
function.
我想根据确认"对话框来实现所选项目的更改.
I would like to implement the change of the selected item as per the Confirmation dialog.
如果确认为是,我可以继续进行选定的更改,否则我将现有项目保留为选定状态并取消更改事件.
If confirms true i can proceed for selected change otherwise I have keep the existing item as selected and cancel the change event.
如何用jQuery实现呢?
How can I implement this with jQuery?
jquery函数
$(function () {
$("#dropdown").change(function () {
var success = confirm('Are you sure want to change the Dropdown ????');
if (success == true) {
alert('Changed');
// do something
}
else {
alert('Not changed');
// Cancel the change event and keep the selected element
}
});
});
要记住的一件事change
功能仅在更改所选项目后才会命中
因此最好考虑在onchange
上实现它-但在jquery中不可用.有什么方法可以实现吗?
One thing to remember change
function hits only after selected item changed
So better to think to implement this on onchange
- but it is not available in jquery. Is there any method to implement this?
推荐答案
好吧,正如Vinu正确指出的那样,只有在select的值实际被更改后,jQuery的change
事件才被触发.您最好这样做:
Well, as Vinu has rightly pointed out, jQuery's change
event is only triggered once the value of the select has actually been changed. You would be better off doing something like this:
var prev_val;
$('#dropdown').focus(function() {
prev_val = $(this).val();
}).change(function() {
$(this).blur() // Firefox fix as suggested by AgDude
var success = confirm('Are you sure you want to change the Dropdown?');
if(success)
{
alert('changed');
// Other changed code would be here...
}
else
{
$(this).val(prev_val);
alert('unchanged');
return false;
}
});
这篇关于jQuery-在下拉菜单的确认对话框上取消更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!