最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

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

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.

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

Share Improve this question edited Jul 20, 2011 at 8:24 Can Gencer 8,8855 gold badges36 silver badges52 bronze badges asked Jul 20, 2011 at 6:27 Royal PintoRoyal Pinto 2,9118 gold badges29 silver badges39 bronze badges 2
  • Did you test this? I didn't, but I think it will work. EDIT: The code you provide works perfectly. What is the question? – Vithozor Commented Jul 20, 2011 at 6:30
  • It will work but the problem is, If I select cancel it will not change to previous value. – Royal Pinto Commented Jul 20, 2011 at 6:34
Add a comment  | 

4 Answers 4

Reset to default 9

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

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/

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

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"));
        }
    });
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;
    }
});

Usage for ASP.NET page:

$("#<%= 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"));
    }
});
发布评论

评论列表(0)

  1. 暂无评论