【问题标题】:Return "True" or "False" values according to buttons pressed - Toastr, JS (no angular)根据按下的按钮返回“True”或“False”值 - Toastr, JS(无角度)
【发布时间】:2020-08-13 09:58:55
【问题描述】:

我构建了一个使用 Toastr 创建警告通知的函数。 (用 JS 编写,不使用 Angular)。

它询问您是否要删除文件。

如果用户按下“YES”按钮,我想返回布尔值:“True”

如果用户按下“NO”按钮,我想返回布尔值:“False”

问题:

当我调用函数 fileAlert 时,我没有得到布尔值。

调用函数:

 async function noticeUser() {
                let promise = new Promise(() => {
                    fileAlert();
                });
                let result = await promise;
                console.log("The result was: "+result); //after execution: result is empty 
            };

函数fileAlert():

<script>

        function fileAlert() { 

                var userAnswer = toastr.warning('Are you sure you want to delete the file?<br /><br /><button type="button" id="yesDelete" class="btn clear">Yes</button>&nbsp;<button type="button" id="noDelete" class="btn clear">No</button>', "Warning");
                var userSaid = true;
                toastr.options = {
                    "closeButton": false,
                    "allowHtml": true,
                    onShown: function (toast) {

                        $('#yesDelete').click(function () {
                            console.log('user clicked yes - he wants to delete the file')
                            toastr.clear(userAnswer);
                            return userSaid;
                        });
                        $('#noDelete').click(function () {
                            console.log('user clicked no - he doesnt want to delete the file')
                            //Deleting warning message from screen.
                            toastr.clear(userAnswer);        
                            //Returning False so that deletion procedure will not occur
                            userSaid= false;
                            return userSaid;
                        });                      

                    },
                    "debug": false,
                    "newestOnTop": false,
                    "progressBar": false,
                    "positionClass": "toast-top-right",
                    "preventDuplicates": false,
                    "showDuration": "300",
                    "hideDuration": "1000",
                    "timeOut": 0,
                    "extendedTimeOut": 0,
                    "showEasing": "swing",
                    "hideEasing": "linear",
                    "showMethod": "fadeIn",
                    "hideMethod": "fadeOut",
                    "tapToDismiss": false
            }  

        };        
    </script>

【问题讨论】:

    标签: javascript asp.net-mvc promise async-await toastr


    【解决方案1】:

    我的解决方案有两个步骤:

    1)我删除了toastr.options里面的onShown,在toastr.options的定义之后调用了onClick函数。

    2) 我将功能 noticeUser 更改为:

    fileAlert().then(result => {
                console.log(result);
                if (result == true) {.....}   });
    

    修复后的fileAlert函数为:

    function fileAlert() { 
            return new Promise((resolve,reject)=>{
    
                var userAnswer = toastr.warning('Are you sure you want to delete the file?<br /><br /><button type="button" id="yesDelete" class="btn clear">Yes</button>&nbsp;<button type="button" id="noDelete" class="btn clear">No</button>', "Warning");               
                toastr.options = {
                    "closeButton": false,
                    "allowHtml": true,                  
                    "debug": false,
                    "newestOnTop": false,
                    "progressBar": false,
                    "positionClass": "toast-top-right",
                    "preventDuplicates": false,
                    "showDuration": "300",
                    "hideDuration": "1000",
                    "timeOut": 0,
                    "extendedTimeOut": 0,
                    "showEasing": "swing",
                    "hideEasing": "linear",
                    "showMethod": "fadeIn",
                    "hideMethod": "fadeOut",
                    "tapToDismiss": false
                }
                $('#yesDelete').click(function () {
                    console.log('user clicked yes - he wants to delete the file')
                    toastr.clear(userAnswer);
                    resolve(true);
                });
                $('#noDelete').click(function () {
                    console.log('user clicked no - he doesnt want to delete the file')
                    //Deleting warning message from screen.
                    toastr.clear(userAnswer);
                    //Returning False so that deletion procedure will not occur                 
                    resolve(false);
                });              
            });               
        };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多