所以我有一个选择选项字段,当我更改选择字段的选项时,将显示确认消息。我的问题是如何防止在取消确认消息后更改选择选项字段的值。
$('.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
}
});
最佳答案
您可以通过存储 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');
关于javascript - 使用jquery取消确认后如何防止更改选择选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39963142/