Javascript - 用户通过HTML输入标签输入来设置一个Javascript变量? [英] Javascript - User input through HTML input tag to set a Javascript variable?
问题描述
我想让我的屏幕上有一个文本框(就像我现在输入的那个文本框一样),然后点击一个提交按钮,它会将您在框中输入的内容发送给javascript,并将其打印出来。这是我的代码这是工作的部分。
< html>
< body>
< input type =textid =userInput=>给我输入< / input>
< button onclick =test()>提交< / button>
< script>
function test()
{
var userInput = document.getElementById(userInput).value;
document.write(userInput);
}
< / script>
< / body>
< / html>
好吧,这很好,但我可以说我想从该文本框和按钮输入,而我已经在一个函数中,不想重新启动函数?
谢谢,
Jake
当脚本运行时,它会阻止页面执行任何操作。您可以使用以下两种方法之一解决此问题:使用 var foo = prompt(Give me input );
,它会给你一个用户输入到一个弹出框的字符串(或者如果他们取消它, null
)) b $ b
I want there to be a textbox on my screen(like the one i'm typing in now) that you can type in then click a submit button and it sends whatever you typed in the box to javascript and javascript prints it out.Here is my code This is the part that works.
<html>
<body>
<input type="text" id="userInput"=>give me input</input>
<button onclick="test()">Submit</button>
<script>
function test()
{
var userInput = document.getElementById("userInput").value;
document.write(userInput);
}
</script>
</body>
</html>
OK so that's nice, but lets say I want input from that textbox and button while I'm already in a function and don't want to restart the function?
Thanks, Jake
When your script is running, it blocks the page from doing anything. You can work around this with one of two ways:
- Use
var foo = prompt("Give me input");
, which will give you the string that the user enters into a popup box (ornull
if they cancel it) - Split your code into two function - run one function to set up the user interface, then provide the second function as a callback that gets run when the user clicks the button.
这篇关于Javascript - 用户通过HTML输入标签输入来设置一个Javascript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!