Javascript 如何防止在取消使用jquery确认后更改选择选项?
因此,我有一个选择选项字段,我对选择字段的选项所做的更改将显示一条确认消息。我的问题是如何防止在取消确认消息后更改选择选项字段的值Javascript 如何防止在取消使用jquery确认后更改选择选项?,javascript,jquery,html,listbox,confirm,Javascript,Jquery,Html,Listbox,Confirm,因此,我有一个选择选项字段,我对选择字段的选项所做的更改将显示一条确认消息。我的问题是如何防止在取消确认消息后更改选择选项字段的值 $('.changeScoreFem').on('change', function(){ if (confirm('Are you sure to change this score?')) { //continue to change the value } else { //else do not change
$('.changeScoreFem').on('change', function(){
if (confirm('Are you sure to change this score?')) {
//continue to change the value
} else {
//else do not change the selecoption field value
}
});
您需要将所选选项存储在变量中,如果确认已取消,请重新选择它
var selected=$(“.changeScoreFem选项:selected”);
$(“.changeScoreFem”)。关于(“更改”,函数(e){
如果(确认(“您确定要更改此分数吗?”))
selected=$(this).find(“选项:selected”);
其他的
选定。道具(“选定”,真);
});代码>
选择1
选择2
选择3
您需要将所选选项存储在变量中,如果确认已取消,请重新选择它
var selected=$(“.changeScoreFem选项:selected”);
$(“.changeScoreFem”)。关于(“更改”,函数(e){
如果(确认(“您确定要更改此分数吗?”))
selected=$(this).find(“选项:selected”);
其他的
选定。道具(“选定”,真);
});代码>
选择1
选择2
选择3
您可以存储select的当前值,然后根据需要撤消它
下面使用自定义事件存储数据,并且在从服务器传递所选项目时,在页面加载时也会触发该自定义事件
var $sel = $('.changeScoreFem').on('change', function(){
if (confirm('Are you sure to change this score?')) {
// store new value
$sel.trigger('update');
} else {
// reset
$sel.val( $sel.data('currVal') );
}
}).on('update', function(){
$(this).data('currVal', $(this).val());
}).trigger('update');
您可以通过存储select的当前值来执行此操作,然后根据需要撤消它
下面使用自定义事件存储数据,并且在从服务器传递所选项目时,在页面加载时也会触发该自定义事件
var $sel = $('.changeScoreFem').on('change', function(){
if (confirm('Are you sure to change this score?')) {
// store new value
$sel.trigger('update');
} else {
// reset
$sel.val( $sel.data('currVal') );
}
}).on('update', function(){
$(this).data('currVal', $(this).val());
}).trigger('update');
可能的重复