使用jquery取消确认后如何防止更改选择选项?

Raf*_*nce 1 html javascript jquery confirm listbox

所以我有一个选择选项字段,当我更改选择字段的选项时,将显示确认消息。我的问题是如何防止在取消确认消息后更改选择选项字段的值。

$('.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
    }
});
Run Code Online (Sandbox Code Playgroud)

cha*_*tfl 6

您可以通过存储 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');
Run Code Online (Sandbox Code Playgroud)

DEMO