【问题标题】:Get userinput in JavaScript [closed]在 JavaScript 中获取用户输入 [关闭]
【发布时间】:2014-04-10 03:42:38
【问题描述】:

我想使用 HTML 中的标签而不是 JavaScript 中的提示函数来获取用户输入。

我也不想在此之上使用另一种语言,例如 php。

编辑 像这样的东西是可取的

<form action="demo_form.asp">
  First name: <input type="text" name="fname"><br>
  <input type="submit" value="Submit">
</form>

但不使用 .asp

【问题讨论】:

  • 这个问题似乎是题外话,因为它是关于学习基础知识

标签: javascript html input


【解决方案1】:
<input type="text" id="input1" />
<button onclick="myJsFunction()"></button>
<script type="text/javascript">
 function myJsFunction(){
    var text=document.getElementById('input1').value;
 }
</script>

【讨论】:

  • 谢谢!这正是我需要的!
  • 内联javascript是不好的做法,应该避免。
  • 我使用 .js 文件来存储实际的 JavaScript,但代码本身可以按我的需要工作。
【解决方案2】:

使用id 属性创建input 标记

<input type="text" id="a" name="b" />

在 javascript 中,您可以通过 id 获取 input 字段的值

var x = document.getElementById('a').value;

【讨论】:

    【解决方案3】:

    对于文本框

    <!DOCTYPE html>
    <html>
        <body>
            <p>Click the button to demonstrate the prompt box.</p>
            <button onclick="myFunction()">Press me</button>
            <p id="demo"></p>
            <script>
             function myFunction()
             {
                var x;
                var person=prompt("Please enter your name","Safa Alrawi");
                if (person!=null)
                {
                  x="Hello " + person + "! How are you today?";
                  document.getElementById("demo").innerHTML=x;
                  }
                }
            </script>
        </body>
    </html>
    

    【讨论】:

      【解决方案4】:

      JavaScript 有三种弹出框:警告框、确认框和提示框。

      <!DOCTYPE html>
      <html>
      <body>
      
      <p>Click the button to display an alert box.</p>
      
      <button onclick="myFunction()">Try it</button>
      
      <script>
      function myFunction()
      {
      alert("I am an alert box!");
      }
      </script>
      
      </body>
      </html>
      

      【讨论】:

      • 请重新阅读问题——OP 想要使用 HTML。其次,内联 javascript 是不好的做法,应该避免。
      猜你喜欢
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多