Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何防止在取消使用jquery确认后更改选择选项?_Javascript_Jquery_Html_Listbox_Confirm - Fatal编程技术网

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');

可能的重复