admin管理员组文章数量:1306140
I am a newbie in HTML5 and javascript. I want an alert box to display data(firstname and lastname). Data must be taken from textboxes on the form. My code is below, but the alertbox doesn't work.
<!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>
I am a newbie in HTML5 and javascript. I want an alert box to display data(firstname and lastname). Data must be taken from textboxes on the form. My code is below, but the alertbox doesn't work.
<!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>
Share
Improve this question
edited Jan 3, 2013 at 9:17
Rikesh
26.5k14 gold badges81 silver badges89 bronze badges
asked Jan 3, 2013 at 9:10
user1942359user1942359
1,6393 gold badges14 silver badges8 bronze badges
3 Answers
Reset to default 2You need to give ids to inputs tags
<form>
<input id="a" type="text"/>
<input id="b" type="text"/>
<input type="button" onclick="display_alert();" />
</form>
and then use them inside your js code:
<script lang="javascript">
function display_alert() {
var fn = document.getElementById('a');
var ln = document.getElementById('b');
alert(fn.value + ' ' + ln.value);
}
</script>
You can use jQuery.
html:
First name: <input type="text" id='fname' /><br>
Last name: <input type="text" id='lname'/><br>
<input type="button" onclick="display_alert();" value="Display alert box">
script:
function display_alert() {
fname = $('#fname').val();
lname = $('#lname').val();
alert(fname+' '+lname);
}
demo
Jquery might be useful for the future.
You need to get values using getElementsByName function in your script.
Try changing your script with below one.
<script>
function display_alert()
{
var fname = document.getElementsByName("fname")[0].value;
var lname = document.getElementsByName("lname")[0].value;
alert(fname + ' ' + lname);
}
</script>
Note: Just for your information, there is no relation with HTML5
to this.
本文标签: javascripthtml5 basic alert and textboxStack Overflow
版权声明:本文标题:javascript - html5 basic alert and textbox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741813543a2398941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论