Javascript - 用户通过HTML输入标签输入来设置一个Javascript变量? [英] Javascript - User input through HTML input tag to set a Javascript variable?

查看:164
本文介绍了Javascript - 用户通过HTML输入标签输入来设置一个Javascript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的屏幕上有一个文本框(就像我现在输入的那个文本框一样),然后点击一个提交按钮,它会将您在框中输入的内容发送给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 (or null 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆