Example 1) Input name and print
示例1)输入名称和打印
Code (JS & HTML):
代码(JS和HTML):
<!DOCTYPE html>
<HTML>
<HEAD>
<SCRIPT>
var name = prompt("Enter Your name:");
var msg = "Welcome "+name;
//alert(msg);
document.write(msg);
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Output
输出量
Example 2) Input two numbers and find their sum
示例2)输入两个数字并求和
Code (JS & HTML):
代码(JS和HTML):
<!DOCTYPE html>
<HTML>
<HEAD>
<SCRIPT>
function Calculate(){
var a=parseInt(document.getElementById("txta").value);
var b=parseInt(document.getElementById("txtb").value);
var c=a+b;
document.getElementById("txtc").value=""+c;
}
</SCRIPT>
</HEAD>
<BODY>
<h2>The Sum Program</h2>
<hr />
<table>
<tr>
<td>
<label>Enter A:</label>
</td>
<td>
<input type="text" name="txta" id="txta" />
</td>
</tr>
<tr>
<td>
<label>Enter B:</label>
</td>
<td>
<input type="text" name="txtb" id="txtb" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value="Calculate" onclick="Calculate()" />
</td>
</tr>
<tr>
<td>
<label>Sum</label>
</td>
<td>
<input type="text" name="txtc" id="txtc" readonly />
</td>
</tr>
</table>
</BODY>
</HTML>
Output
输出量
翻译自: https://www.includehelp.com/code-snippets/input-value-from-the-user-using-prompt.aspx