简体   繁体   English

在Javascript中使用Window.prompt()

[英]Using Window.prompt() in Javascript

I have the following code: 我有以下代码:

<!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>

Where it shows a popup box asking the user to enter a name, and john is the default name. 它显示一个弹出框,要求用户输入名称,而john是默认名称。

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. 我要这样做,以便用户可以单击弹出窗口中显示的名称之一(John,mike,steve,oliver等),然后单击该名称将导致该名称出现在可键入字段中。 Is this possible? 这可能吗? Would I need a different kind of prompt in Javascript? 我需要Java语言中的其他提示吗?

I essentially don't want to type in a value, but be able to click it through a list of names, and click okay or cancel accordingly. 我基本上不想输入值,但是可以在名称列表中单击它,然后单击“确定”或相应地取消。

So for example, if the user clicks the "Mike" as a link in the popup, it should fill the box at the bottom with the name "Mike", by clicking it instead of having to type it. 因此,例如,如果用户在弹出窗口中单击“ Mike”作为链接,则应该通过单击它而不是键入来将名称“ Mike”填充在底部的框中​​。

Hopefully that makes sense, any help would be much appreciated. 希望这是有道理的,任何帮助将不胜感激。

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. 我要这样做,以便用户可以单击弹出窗口中显示的名称之一(John,mike,steve,oliver等),然后单击该名称将导致该名称出现在可键入字段中。 Is this possible? 这可能吗?

Not with prompt() . 不能使用prompt() You will need to make your own UI in HTML, with a form that has the layout you want. 您将需要使用HTML制作自己的UI,并使用具有所需布局的表单。

Also, an unrelated note... this code is dangerous: 另外,不相关的说明...此代码很危险:

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

When you concatenate text into HTML, you allow people to enter malicious code, or just normal reserved characters that may break your HTML. 当您将文本连接成HTML时,您会允许人们输入恶意代码,或者只是可能破坏HTML的普通保留字符。 Make sure you're setting the text instead. 确保您设置的是文字。

Check Here 在这里检查

I have added a answer on the basis of @brad 我在@brad的基础上添加了一个答案

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?";
    }
}

 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 src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> <p>Click the button to demonstrate the prompt box.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM