Javascript提示()替代 [英] Javascript Prompt() Alternative

查看:84
本文介绍了Javascript提示()替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的JavaScript。

  var userName = prompt(你叫什么名字?); 
document.write(Hello+ userName +。);

是否有另一种方式获取用户的输入(名称),而不是通过弹出框?
如何在网站中实现文本框来获取用户名?



这是怎么回事?

 < section class =content> 
< input id =nametype =text>
< button onclick =userName()>提交< / button>
< p id =user>< / p>
< script>
function userName(){
var name = document.getElementById(name)。value;
user.innerHTML =Hello++ name}
< / script>
< / section>


解决方案

一段简单的HTML创建文本字段一个按钮:

 < input id =nametype =text> 
< button onclick =go()> Go< / button>

一个简单的脚本可以在点击按钮时调用它:

  function go(){
var text = document.getElementById(name).value;
alert(用户输入'+ text +');
}

演示: http://jsfiddle.net/jfriend00/V74vV/






简而言之,您可以将数据输入字段放入页面中。然后,您将某个事件处理程序连接到某种用户事件(如单击按钮),并在该代码中从数据输入字段中获取值并根据需要执行任何操作。



如果您想了解 input 标签的所有不同类型,请阅读 here。



您可能还想阅读< a href =https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById =nofollow> document.getElementById(xxx) ,它允许你在页面中找到你已经分配了id的元素,然后从它们中获取属性。


Simple JavaScript.

var userName = prompt ("What's your name?");
document.write ("Hello " + userName + ".");

Is there an alternative way to get the user's input (name) than through a pop up box? How would I go about implementing a text box within the site to obtain the user's name?

How is this?

<section class="content">
<input id="name" type="text">
<button onclick="userName()">Submit</button>
<p id="user"></p>
<script>
function userName() {
var name = document.getElementById("name").value;
user.innerHTML = "Hello" + " " + name}
</script>
</section>

解决方案

A simple piece of HTML to create a text field and a button:

<input id="name" type="text">
<button onclick="go()">Go</button>

And a simple script to go with it that is called when the button is clicked:

function go() {
    var text = document.getElementById("name").value;
    alert("The user typed '" + text + "'");
}

Working demo: http://jsfiddle.net/jfriend00/V74vV/


In a nutshell, you put data entry fields into your page. Then you hook up an event handler to some sort of user event (like the click of a button) and in that code, you fetch the value from the data entry field and do whatever you want with it.

If you want to read about all the different types of input tags, read here.

You probably also want to read about document.getElementById("xxx") which allows you to find elements in your page that you've assigned an id to and then fetch properties from them.

这篇关于Javascript提示()替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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