【问题标题】:jQuery - cancel change event on confirmation dialog for a DropdownjQuery - 取消下拉确认对话框上的更改事件
【发布时间】:2011-07-13 10:52:13
【问题描述】:

我有一个下拉菜单,我有 jQuery change 函数。

我想根据确认对话框来实现所选项目的更改。

如果确认为真,我可以继续进行选定的更改,否则我将现有项目保持为选中状态并取消更改事件。

如何用 jQuery 实现这个?

jquery函数

$(function () {
    $("#dropdown").change(function () {
        var success = confirm('Are you sure want to change the Dropdown ????');
        if (success == true) {
            alert('Changed');  
            // do something                  
        }
        else {
            alert('Not changed');
            // Cancel the change event and keep the selected element
        }
    });
});

要记住的一件事 change 函数仅在所选项目更改后才会触发 所以最好考虑在onchange 上实现它——但它在 jquery 中不可用。有什么方法可以实现吗?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

好吧,正如 Vinu 正确指出的那样,jQuery 的 change 事件只有在 select 的值实际更改后才会触发。你最好做这样的事情:

var prev_val;

$('#dropdown').focus(function() {
    prev_val = $(this).val();
}).change(function() {
     $(this).blur() // Firefox fix as suggested by AgDude
    var success = confirm('Are you sure you want to change the Dropdown?');
    if(success)
    {
        alert('changed');
        // Other changed code would be here...
    }  
    else
    {
        $(this).val(prev_val);
        alert('unchanged');
        return false; 
    }
});

【讨论】:

  • 是的,我知道,但是当你发布你的时,我正在发布我的。刷新页面后才看到...
  • 点赞这个。如果用户使用 Tab 导航页面,它也可以工作,这与前一个不同。
  • 这在 Firefox 上对我不起作用(没有尝试过其他任何方法)。由于某种原因,当确认框被确认/取消并且在触发更改之前重新触发焦点事件导致 prev_val 的值不正确。点击即可正常工作。
  • IIRC,某些浏览器会在显示模式对话框时将焦点从输入中移开。确认后,Firefox 可能正在恢复焦点,因此再次运行该功能。在显示确认之前,请尝试从字段中移除焦点。
  • BenM 关于移除焦点的评论是正确的。使用 $(this).blur()
【解决方案2】:

和我在这里做的一样吗?

http://jsfiddle.net/Swader/gbdMT/

只要用户单击选择框,只需保存该值,如果 onchange 确认返回 false,则恢复为该值。

这是我小提琴中的代码:

var lastValue;

$("#changer").bind("click", function(e) {
    lastValue = $(this).val();
}).bind("change", function(e) {
    changeConfirmation = confirm("Really?");
    if (changeConfirmation) {
        // Proceed as planned
    } else {
        $(this).val(lastValue);
    }
});

【讨论】:

  • Hai Swader,我在工作...非常感谢您的帮助 :)
  • @Vinu 没问题,很高兴我能帮上忙。如果它解决了您的问题,请不要忘记将答案标记为正确!祝你好运!
【解决方案3】:

使用下面的代码,我已经测试过它和它的工作原理

var prev_val;
$('.dropdown').focus(function() {
    prev_val = $(this).val();
}).change(function(){
            $(this).unbind('focus');
            var conf = confirm('Are you sure want to change status ?');

            if(conf == true){
                //your code
            }
            else{
                $(this).val(prev_val);
                $(this).bind('focus');
                return false;
            }
});

【讨论】:

  • bind 已被弃用,仅使用 $(this).focus() 代替。
【解决方案4】:

下面的代码是为单选按钮实现的。我认为这个稍作改动的代码也可以用于下拉菜单。

      <input type="radio" class="selected" value="test1" name="test1" 
      checked="checked" /><label>test1</label>

      <input type="radio" name="test1" value="test2" /><label>
      test2</label>

      <input type="radio" name="test1" value="test3" /><label>
      test3</label>

</div>

 $("input[name='test1']").change(function() {
        var response = confirm("do you want to perform selection change");
        if (response) {
            var container = $(this).closest("div.selection");
            //console.log(container);
            //console.log("old sel =>" + $(container).find(".selected").val());
            $(container).find(".selected").removeClass("selected");
            $(this).addClass("selected");
            //console.log($(this).val());
            console.log("new sel =>" + $(container).find(".selected").val());
        }
        else {
            var container = $(this).closest("div.selection");
            $(this).prop("checked", false);
            $(container).find(".selected").prop("checked", true);
        }
    });

【讨论】:

    【解决方案5】:

    据我所知,您必须自己处理,这可能会有所帮助:

    <select onFocus="this.oldIndex = this.selectedIndex" onChange="if(!confirm('Are you sure?'))this.selectedIndex = this.oldIndex">
    

    【讨论】:

      【解决方案6】:

      简单地做

      $("#dropdown").change(function () {
          var success = confirm('Are you sure want to change the Dropdown ????');
          if (success == true) {
              // do something                  
          }
          else {
              return false; // will set the value to previous selected
          }
      }); 
      

      【讨论】:

        【解决方案7】:

        模糊、聚焦和存储以前的值似乎有点麻烦。我通过将侦听器不是附加到选择,而是附加到选项来解决了这个问题:

        $("#id").on('mousedown','option',confirmChange);
        

        那么,

        function confirmChange(e){
            var value = $(this).val(),
                c = confirm('Are you sure you want to change?');
        
            if(c)
                $(this).parent().val(value);//can trigger .change() here too if necessary
            else
                e.preventDefault();
        }
        

        当然可以进行优化,但这是大体思路。

        【讨论】:

        • 是的,很好,但只是想注意到这错过了您使用代码动态更改选择的情况。
        猜你喜欢
        • 2016-10-09
        • 2017-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-24
        • 2012-03-23
        • 1970-01-01
        相关资源
        最近更新 更多