该函数位于js函数的一个非常大的层次结构中,所以不能从视图中使用html,否则我将丢失所有参数,因为每个脚本中都传递了许多参数。
对这个提示符使用bootbox.js --因为它在提示符中隐藏了密码--不工作的代码。我需要等待回调,而不是在显示提示后完成剩余脚本的执行。
function authorize() {
alert("Script has started...");
var final_result = 0;
bootbox.prompt({
title: "This is a prompt with a password input!",
inputType: 'password',
callback: function(result)
{
if (result!=null)
{
final_result = 1;
} else {
final_result = 0;
}
}
});
alert("calling XYZ");
function xyz();
alert("Script has finished... final result is" + final_result);
}具有默认JavaScript提示符的工作代码--想要这样(使用引导程序as来掩盖输入,因为我正在使用它作为密码):
function authorize() {
alert("Script has started...");
var final_result = 0;
let text = prompt("This is a prompt with a password input!", "")
if (text!= null) {
final_result = 1;
} else {
final_result = 0;
}
alert("calling xyz" );
function xyz();
alert("Script has finished... final result is" + final_result );
}发布于 2022-10-04 00:45:38
这在文档中有很多次介绍:
与本机警报、确认或提示不同,所有的引导调制解调器(因此也包括引导箱调制解调器)都是异步的;这意味着,引导程序生成的事件是非阻塞事件。由于这个限制,必须在对话框的回调函数中调用不应该在用户拒绝您的对话框之前对其进行评估的代码。
考虑到你的例子,我假设你想要的是:
function authorize() {
alert("Script has started...");
let final_result = 0;
bootbox.prompt({
title: "This is a prompt with a password input!",
inputType: 'password',
callback: function(result) {
if (result != null) {
final_result = 1;
}
else {
final_result = 0;
}
// call xyz() here, with your prompt's value
xyz(final_result);
}
});
}
// Changed this to accept a parameter, to match what
// it seems you're trying to do
function xyz(final_result) {
alert("Script has finished... final result is" + final_result);
}重申:<div>引导模式只是定位了元素,而 modal() 函数只是隐藏和显示它们。
如果你真的想要一个可定制的提示符来阻止程序的执行,那么你唯一的选择就是<dialog> (见MDN)。不过,它只是最近才获得了足够的浏览器支持,可以使用,所以您可以自己承担使用的风险。
https://stackoverflow.com/questions/73917520
复制相似问题