如何从提示中获得响应

时间:2015-07-12 05:28:16

标签: javascript html html5

如何从提示窗口获取响应并将其显示在网页的某个位置?

我现在的代码是:

<html>

<head>

	<center>

		<FORM> 

			<INPUT type="button" value="Click Me." name="button" onClick="alert('I Like Waffles')">

			<INPUT type="button" value="Whats Your Favorite Food" name="button" onClick="prompt('Whats Your Favorite Food?')">

		</FORM>

	</center>

</head>

</html>

我想知道如何从提示中获取响应并将其放在html中。

2 个答案:

答案 0 :(得分:1)

提示将返回值。

 alert(prompt('Whats Your Favorite Food?'))

&#13;
&#13;
<html>

<head>

	<center>

		<FORM> 

			<INPUT type="button" value="Click Me." name="button" onClick="alert('I Like Waffles')">

			<INPUT type="button" value="Whats Your Favorite Food" name="button" onClick="alert(prompt('Whats Your Favorite Food?'))">

		</FORM>

	</center>

</head>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

工作示例:

<html>

<head>

<center>
    <script>
    function gen(){
        var a = prompt("What's your favorite food?");
        document.getElementById("demo").innerHTML = a;
    }
    </script>

    <FORM> 

        <INPUT type="button" value="Click Me." name="button" id="button" onClick="alert('I Like Waffles')">

        <INPUT type="button" value="Whats Your Favorite Food" name="button" onClick="gen();">
        <p id="demo"></p>

    </FORM>

</center>

查看此Fiddle