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

Roy*_*nto 7 html javascript jquery

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

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

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

谢谢

Can*_*cer 9

您应该能够将以前的值存储在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;
      }
});
Run Code Online (Sandbox Code Playgroud)

请参阅:http://jsfiddle.net/w9JYX/14/

更新:我更新了代码以更一般地在一组下拉控件上工作,并且还删除了单击处理程序.