Return "True" 或 "False" 值根据按下的按钮 - Toastr,JS(无 angular)
Return "True" or "False" values according to buttons pressed - Toastr, JS (no angular)
我构建了一个使用 Toastr 创建警告通知的函数。 (用JS写的,没有用Angular)。
询问是否删除文件
如果用户按下 "YES" 按钮,我想要 return 布尔值:"True"
如果用户按下 "NO" 按钮,我想要 return 布尔值:"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> <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>
我的解决方案有 2 个步骤:
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> <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);
});
});
};
我构建了一个使用 Toastr 创建警告通知的函数。 (用JS写的,没有用Angular)。
询问是否删除文件
如果用户按下 "YES" 按钮,我想要 return 布尔值:"True"
如果用户按下 "NO" 按钮,我想要 return 布尔值:"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> <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>
我的解决方案有 2 个步骤:
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> <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);
});
});
};