4

我正在使用 jquery toastr。到目前为止一切都很好。我可以很好地展示和关闭祝酒词。我希望能够唯一地识别每个吐司。并在 onHidden 函数中使用该唯一标识符。有没有人这样做过?为 toastr 类或关闭的 toast 周围的 div 调用关闭事件的 jquery 是更好的选择吗?

    var mes = 'My name is Inigo Montoya.<input type="hidden" id="announcementId" value="1"/>' +
       '<input type="hidden" id="userId" value="'+ userid +'"/> ';

    var mes1 = 'Princess Bride<input type="hidden" id="announcementId2" value="2"/>'+
       '<input type="hidden" id="userId1" value="'+ userid +'"/> ';

    var mes2 = 'Man in Black<input type="hidden" id="announcementId2" value="3"/>'+
       '<input type="hidden" id="userId2" value="'+ userid +'"/> ';

   var mes3 = 'Inconcivable!<input type="hidden" id="announcementId3" value="4"/>'+
       '<input type="hidden" id="userId3" value="'+ userid +'"/> ';

toastr.options = {
  "closeButton": false,
  "debug": false,
  "positionClass": "toast-top-full-width",
  "showDuration": "300",
  "hideDuration": "1000",
  "timeOut": "0",
  "extendedTimeOut": "0",
  "showEasing": "swing",
  "hideEasing": "linear",
  "showMethod": "fadeIn",
  "hideMethod": "fadeOut"
};

toastr.options.onHidden = function(item) { 
//GET UNIQUE TOAST ID'S HERE
        var val = 1;//$this.find("#announcemntId").val();
        alert("CLOSED " + item); 
}

toastr.error(mes, "First Toast");
toastr.error(mes1, "Second Toast");
toastr.error(mes2, "Third Toast");
toastr.error(mes3, "Fourth Toast");
4

3 回答 3

8

您可以传递第三个参数,即选项覆盖

toastr.error('Some error', 'Error', { onHidden: function() {
         console.log('Error toast hidden.')
}});

或者修改全局 onHidden

var onHiddenToast = function () { 
        console.log("onHidden");
}
toastr.options.onHidden = onHiddenToast;

你也可以简单地通过引用它来获得吐司

var myToast = toastr.info("Some info");
//do what you want with myToast
于 2014-09-15T07:06:26.273 回答
0

万一以后有人遇到这个问题,这是我的解决方案。从 json 加载吐司。每个 toast 都在它自己唯一的 div 中(信息、错误、警告、成功),并且每个都有一个分配给它的类。我在 toast 中的每条消息中分配了隐藏的属性以及我需要的值。

$.ajax({
    dataType: "json",
    url: '/announcements/getannouncements/userid/' + userid,
    success: function (data) {
        $.each(data, function (i, val) {
            var mes = '<input type="hidden" id="userId" value="' + userid + '"/>' +
                '<input type="hidden" id="announcementId" value="' + val.id + '"/>' +
                'Client:  ' + val.client + '</br>' + val.announcement;
            var title = val.title;
            toastr.error(mes, title); //info, success, warning, error
        });
    },
    error: function () {
        alert("Could not get announcments");
    }
});

由于单击 div 时会关闭 toast,因此我可以捕获单击的 div 类,找到公告和用户 ID 并执行我的逻辑

//class could be warning, error, info, success : we only use error
$(".toast-error").live('click', function () {
    var userId = $(this).find("#userId").val();
    var announcementId = $(this).find("#announcementId").val();
    var url = '/announcements/acknowledge/userid/' + userId + '/announceid/' + announcementId;
    // ajax call to the controller to write the timestamp of the user clicking the toast announcement
    $.ajax({
        dataType: "json",
        url: url,
        success: function (data) {
            if (data == '1') {
                alert("Successfully acknowledged");
            }
            else {
                alert("Data error");
            }
        },
        error: function () {
        }
    });
}); 
于 2014-09-15T05:49:48.973 回答
0

我不太明白这些答案,所以我添加我自己的。我使用 jquery 创建了一个 onclose 函数,(当有人单击 toast-close 按钮​​时)像这样 -

$('.toast-close-button').click(function() {
*someCodeHere 
});
于 2019-05-29T16:24:37.757 回答