【问题标题】:Prevent Javascript confirm() on Checkbox State Change在复选框状态更改时防止 Javascript 确认()
【发布时间】:2015-11-19 20:49:35
【问题描述】:

我有一个复选框(它使用 Bootstrap Switch 库作为开/关切换)在 AJAX 的帮助下激活和停用用户。

当用户取消选中该框时,此函数被触发:

$('.user-status-ckbx').on('switchChange.bootstrapSwitch'...

confirm() 对话框弹出询问用户是否确定要取消/激活用户。如果用户单击“否”,则按钮将使用以下命令返回其原始状态:

$("#" + e.currentTarget.id).bootstrapSwitch('toggleState');

我遇到的问题是,每次 toggleState() 运行时,switchChange.bootstrapSwitch 也会再次运行。这会发送一个非结束的 confirm() 消息,只有在用户确认消息时才会消失。

有没有一种有效的方法来防止 switchChange.bootstrapSwitch 方法基于真实用户点击与以编程方式生成的切换来运行?

我已经试过了:

e.originalEvent !== undefined

e.which

正如其他类似问题中所建议的那样,但这些都不起作用,它们甚至都没有出现在“e”对象中......

<script>  
    $(".user-status-ckbx").bootstrapSwitch('size', 'mini');
    $(".user-status-ckbx").bootstrapSwitch('onText', 'I');
    $(".user-status-ckbx").bootstrapSwitch('offText', 'O');

    //ajax to activate/deactivate user
    $('.user-status-ckbx').on('switchChange.bootstrapSwitch', function(e){

        var currentDiv = $("#" + e.currentTarget.id).bootstrapSwitch('state');
        if( currentDiv == false){
          var confirmed = confirm("Are you sure you wish to deactivate this user? They will no longer be able to access any forms.");
          if(confirmed == true){
            changeActivationStatus($(this).val());
          } else {
            $("#" + e.currentTarget.id).bootstrapSwitch('toggleState');
          }
        } else {
          var confirmed = confirm("Are you sure you wish to activate this user? Deactivated users which were previously active will have the same permissions prior to their de-activation unless changed manually.");
          if(confirmed == true){
            changeActivationStatus($(this).val());
          } else {
            $("#" + e.currentTarget.id).bootstrapSwitch('toggleState');
          }
        }
    });

    function changeActivationStatus(userId){
      $.post("{{ path('isactive') }}", {userId: userId})
          .done(function(data){
            console.log("Finished updating " + userId);
          })
          .fail(function(){
            console.log("User could not be updated");
          });
    };
 </script>

【问题讨论】:

    标签: javascript jquery bootstrap-switch


    【解决方案1】:

    有一种方法可以在以编程方式切换时阻止该事件。

    您必须向 Bootstrap 开关添加选项:

    var options = {
            onSwitchChange: function (event, state) {
                // Return false to prevent the toggle from switching.
                return false;
            }
        };
    $(".user-status-ckbx").bootstrapSwitch(options);
    

    当以编程方式切换按钮时,您必须添加第二个参数:

    $("#" + e.currentTarget.id).bootstrapSwitch('toggleState', true);
    

    JSFiddle:https://jsfiddle.net/Ravvy/npz8j3pb/

    【讨论】:

    • 来自 Bootstrap Switch Doc:“方法 toggleState 可以接收可选的第二个参数 skip。如果为 true,则不执行 switchChange 事件。默认为 false。”事实证明,您甚至不需要将 onSwitchChange 选项设置为 false。这样做会产生相反的效果,即当用户点击“确认”时不切换状态。我将相应地编辑您的答案。谢谢
    【解决方案2】:
    $(".uid-toggle").change(function (event) {
        var option = confirm('Are you sure, you want to change status?');
        console.log(`$(this).prop('checked')`);
        if (option) {
        } else {
            if ($(this).prop('checked')) {
                $(this).prop('checked', !$(this).prop('checked'));                
                $(this).parent().removeClass('btn-primary off');
                $(this).parent().addClass('btn-danger off');
            } else {
                $(this).prop('checked', !$(this).prop('checked'));
                $(this).parent().addClass('btn-primary off');
                $(this).parent().removeClass('btn-danger off');
            }
        }
    });
    

    【讨论】:

    • 请说明为什么这可以解决问题,而不仅仅是包含代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2015-08-19
    • 2015-11-20
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多