javascript 取消下拉列表更改事件时如何设置以前的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6757826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:44:58  来源:igfitidea点击:

How to set previous value when cancelling a drop-down list change event

javascriptjqueryhtml

提问by Royal Pinto

I am designing a html page. I want to show a confirmation msg on changing a drop down element using jquery or javascript. Please help to do this.

我正在设计一个 html 页面。我想在使用 jquery 或 javascript 更改下拉元素时显示确认消息。请帮助做到这一点。

I have code which will ask confirmation. On selecting cancel it will not select previous item of Drop down.

我有要求确认的代码。在选择取消时,它不会选择下拉的上一项。

$("#dropdownId").change(function(e) 
{
        if($(this).val() == "40")
        {
            if(confirm("Are you sure"))
                return true;
            else
                return false;
        }
});

Thanks

谢谢

回答by Can Gencer

You should be able to store the previous value on the click event and set it back on the change event:

您应该能够在 click 事件上存储先前的值并将其重新设置在 change 事件上:

var setLastSelected = function(element) {
   $(element).data('lastSelected', $(element).find("option:selected"));
};

$("select").each(function () {
   setLastSelected(this);
});

$("select").change(function(){        
      if(confirm("Are you sure")) { 
          setLastSelected(this);
          return true; 
      }
      else {
         $(this).data('lastSelected').attr("selected", true);
         return false;
      }
});

See: http://jsfiddle.net/w9JYX/14/

见:http: //jsfiddle.net/w9JYX/14/

Update: I updated the code to work more generically on a set of dropdown controls and also removed the click handler.

更新:我更新了代码以在一组下拉控件上更通用地工作,并删除了点击处理程序。

回答by Shef

var previous_option = $('#dropdownId option:selected');
$("#dropdownId").change(function(e){
    var $this = $(this),
        selected = $this.find('option:selected');
    if($this.val() == "40"){
        if(confirm("Are you sure")){
            previous_option = selected;
            return true;
        } else{
            selected.removeAttr('selected');
            previous_option.attr('selected', 'selected');
        }
    } else{
        previous_option = selected;
    }
});

回答by DShook

Here's a bit tighter solution along the same lines without having to create global variables or other functions:

这里有一个更严格的解决方案,无需创建全局变量或其他函数:

$('#dropdownId')
    .on('focus', function () {
        $(this).data("prev", $(this).val());
    })
    .change(function () {
        if (confirm('Are you sure?')) {
            //normal case where the dropdown changes
            $(this).data("prev", $(this).val());
        } else {
            //if the user doesn't confirm reset the dropdown back to what it was
            $(this).val($(this).data("prev"));
        }
    });

回答by RFE Petr

Usage for ASP.NET page:

ASP.NET 页面的用法:

$("#<%= dropdownId.ClientID %>")
.on('focus', function () {
    $(this).data("prev", $(this).val());
})
.change(function () {
    if (confirm('Are you sure?')) {
        $(this).data("prev", $(this).val());
    } else {
        $(this).val($(this).data("prev"));
    }
});