我正在学习javascript,我现在正在玩各种功能,我正在尝试提醒用户他们输入了什么,但当我输入名字和姓氏和电子邮件并按下订阅时,警告框显示:
Hello [object HTMLInputElement] [object HTMLInputElement]! Your email [object HTMLInputElement] was succesfully registered ...
这是我的代码:
<script>
function printMessage(firstNameID,lastNameID,emailID){
var firstName = document.getElementById(firstNameID);
var lastName = document.getElementById(lastNameID);
var email = document.getElementById(emailID);
alert("Hello " + firstName + " " + lastName + "! Your email " + email + " was succesfully registered ...");
}
</script>
<form name="subscribeform">
First Name: <br>
<input type="textbox" id="firstname" form="subscribeform"><br>
Last Name: <br>
<input type="textbox" id="lastname" form="subscribeform"><br>
Email: <br>
<input type="textbox" id="email" form="subscribeform"><br><br>
<button id="subscribe" form="subscribeform" onclick="printMessage('firstname','lastname','email');">Subscribe, you know you want to ...</button>
</form>
答案 0 :(得分:3)
您需要value
元素,而不是元素本身。
alert("Hello " + firstName.value + " " + lastName.value + "! Your email " + email.value + " was succesfully registered ...");
...或
var firstName = document.getElementById(firstNameID).value;
var lastName = document.getElementById(lastNameID).value;
var email = document.getElementById(emailID).value;