onclick of button 仅显示一次 toastr 并将错误消息分组在 toastr 中

onclick of button show toastr only once and group the error messages in toastr

我正在使用 toastr jquery 插件。下面是 link

https://github.com/scottoffen/jquery.toaster

我在这里面临 2 个问题

  1. 目前点击按钮显示多个通知(请参考附图)

    预期:每次点击应该显示一个通知,如果有通知则不显示另一个。

    1. 对多个通知进行分组的方法

    预计:将名称和密码组合为一个通知

代码

$(".actions").on("click", ".startWorkflowD", function() {

    //Disable Start workflow button
    $('.startWorkflowD').attr('disabled', 'disabled');

    var userDisplayName = $("#HEADER_USER_MENU_POPUP_text").text().toLowerCase();
    var actualUserDisplayName = $("#name").val().toLowerCase();

    if (userDisplayName != actualUserDisplayName) {
        $.toaster({
            priority: 'warning',
            title: 'Authentication Failed',
            message: '<br/>' + 'Please enter valid Name (First name and Last name), to start the workflow.'
        });

        //Enable start workflow button
        $('.startWorkflowD').removeAttr("disabled");

        return;
    }

    var password = $("#Password");

    if (password.val().length == 0) {
        // password is empty
        $.toaster({
            priority: 'warning',
            title: 'Authentication Failed',
            message: '<br/>' + 'Please enter valid Password, to start the workflow.'
        });

        //Enable start workflow button
        $('.startWorkflowD').removeAttr("disabled");
    }

});

感谢任何帮助

我试过下面的 Whosebug 答案,我的 toastr 在选项中没有 **"preventDuplicates": true,**

toastr (jquery) must only show once

可能对某些人有帮助。 这是我做的示例演示。

  • 不允许重复
  • 进度条

带有图标的选项

<input type="text" name="name" id="name" placeholder="Name" />
<br>
<input type="password" name="Password" id="Password" placeholder="Password" />
<br>
<input type="button" class="actions" value="Login" />

<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css" rel="stylesheet"
    type="text/css">

<script src="https://code.jquery.com/jquery-3.4.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>

<script>
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "newestOnTop": false,
        "progressBar": true,
        "positionClass": "toast-top-right",
        "preventDuplicates": true,
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }


    $(".actions").on("click", function () {
        var name = $("#name").val();
        var pwd = $("#Password").val();
        var errmsg = "";
        if (name == "") {
            errmsg = "Please enter valid Name (First name and Last name)";
        }
        if (pwd == "") {
            errmsg += " Please enter valid Password";
        }
        if (errmsg != "")
            toastr.error(errmsg + ' to start the workflow.');
    });
</script>