gpt4 book ai didi

JavaScript/HTML : How to display user input into html body?

转载 作者:行者123 更新时间:2023-11-30 08:51:50 24 4
gpt4 key购买 nike

这是我的 html 表单:

<label>Name:</label><input type ="text" input id="myName" /><br>
<label>Street:</label><input type="text" input id="myStreet" /><br>
<label>City:</label><input type="text" input id="myCity" /><br>
<label>State:</label> <input type="text" input id="myState" /><br>
<label>Zip:</label> <input type="text" input id="myZip" /><br>

<input type="submit" value="Submit" onclick="myAlert()">
</form>
//my myAlertFunc (from external file)

function myAlert(){
var person = new Person(document.getElementById('myName').value, document.getElementById("myStreet").value, document.getElementById("myCity").value, document.getElementById("myState").value, document.getElementById("myZip").value);
alert(person.getFullAddress());
}

//and my getFullAddress proto from my Person class

Person.prototype.getFullAddress = function () {
return ("Hi, my name is" + " " + this.name + "," + " " + "and my address is:" + " " + this.street + "," + this.city + "," + this.state + "," + this.zip);
};

现在,单击时,myAlertFunc 会提醒用户输入值。如何在我的 html 正文中显示这些值?我知道我应该使用一个只有输入 id 的元素并使用 document.getElementById();调用那个 id 但我不知道之后该怎么做。

提前致谢!

最佳答案

您要查找的属性是 innerHTML: http://www.w3schools.com/jsref/prop_html_innerhtml.asp

所以,您的代码应该是这样的:

// alert(person.getFullAddress());
document.getElementById("resultDiv").innerHTML = person.getFullAddress();

其中“resultDiv”是您希望显示结果的元素的 ID。

祝你好运!

关于JavaScript/HTML : How to display user input into html body?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17179049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com