我是 HTML5 和 javascript 的新手。 我想要一个警告框来显示数据(名字和姓氏)。 数据必须取自表单上的文本框。 我的代码在下面,但警告框不起作用。
<!DOCTYPE html>
<html>
<body>
<form action="demo_form.asp" method="get">
First name: <input type="text" name="fname" /><br>
Last name: <input type="text" name="lname" /><br>
</form>
<script>
function display_alert()
{
alert(fname +lname);
}
</script>
<input type="button" onclick="display_alert()" value="Display alert box">
</body>
</html>
最佳答案
您需要为输入标签提供 id
<form>
<input id="a" type="text"/>
<input id="b" type="text"/>
<input type="button" onclick="display_alert();" />
</form>
然后在您的 js 代码中使用它们:
<script lang="javascript">
function display_alert() {
var fn = document.getElementById('a');
var ln = document.getElementById('b');
alert(fn.value + ' ' + ln.value);
}
</script>
关于javascript - html5 基本警报和文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14135837/