javascript - 在 Javascript 中使用 Window.prompt()

标签 javascript jquery html popupwindow

我有以下代码:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var person = prompt("Choose a name\nJohn\nMike\nSteve\nOliver", "John");

    if (person != null) {
        document.getElementById("demo").innerHTML =
        "Hello " + person + "! How are you today?";
    }
}
</script>

</body>
</html>

其中显示一个弹出框,要求用户输入名称,john 是默认名称。

我想让用户可以单击弹出窗口中显示的名称之一(约翰、迈克、史蒂夫、奥利弗等),然后单击将导致该名称出现在可输入字段中。这可能吗?我需要使用不同类型的 Javascript 提示吗?

我本质上不想输入值,但能够通过名称列表单击它,然后相应地单击“确定”或取消。

例如,如果用户在弹出窗口中单击“Mike”作为链接,则应该通过单击而不是键入名称来填充底部的框,其中包含名称“Mike”。

希望这是有道理的,任何帮助将不胜感激。

最佳答案

I want to make it so the user can click one of the names presented in the popup (John,mike,steve,oliver, etc) and that click would then result in the name appearing in the typable field. Is this possible?

不适用于prompt()。您需要使用 HTML 格式创建自己的 UI,并使用具有所需布局的表单。

另外,一个不相关的注释...此代码很危险:

document.getElementById("demo").innerHTML =
    "Hello " + person + "! How are you today?";

当您将文本连接到 HTML 中时,就会允许人们输入恶意代码或可能破坏您的 HTML 的普通保留字符。确保您正在设置文本。

关于javascript - 在 Javascript 中使用 Window.prompt(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40332727/

相关文章:

javascript - 异步回调返回null

javascript - 使用 jquery 遍历对象数组

jquery - Bootstrap 4 轮播在选项卡内不起作用

Javascript - 无法显示一周中超过 4 天?

javascript - 多参数 URL CORS 错误

jquery - 当有滚动条时如何获得完整的窗口高度?

javascript - jscrollpane设置垂直滚动条轨道高度

html/css 简单图像对齐问题

html - 如何显示 block 内联?

javascript - jQuery .animate() - 不工作