Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript/HTML:如何将用户输入显示到HTML正文中?_Javascript_Html - Fatal编程技术网

JavaScript/HTML:如何将用户输入显示到HTML正文中?

JavaScript/HTML:如何将用户输入显示到HTML正文中?,javascript,html,Javascript,Html,这是我的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" inp

这是我的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(来自外部文件) 函数myAlert(){ var person=newperson(document.getElementById('myName')。value,document.getElementById(“myStreet”)。value,document.getElementById(“myCity”)。value,document.getElementById(“myState”)。value,document.getElementById(“myZip”)。value); 警报(person.getFullAddress()); } //还有我的Person类的getFullAddress原型 Person.prototype.getFullAddress=函数(){ return(“嗨,我的名字是“+”+this.name+”,“+”+”,我的地址是“+”+this.street+”,“+this.city+”,“+this.state+”,“+this.zip”); };
现在,onclick,myAlertFunc会提醒用户输入值。如何在html正文中显示这些值?我知道我应该使用一个只有输入id的元素,并使用document.getElementById();但我不知道之后该怎么办


提前谢谢

您可以设置元素的innerHTML或其值(如果它是输入)

您要查找的属性是innerHTML:

因此,您的代码应该大致如下:

// alert(person.getFullAddress());
document.getElementById("resultDiv").innerHTML = person.getFullAddress();
其中“resultDiv”是要显示结果的元素的ID


祝你好运

我们需要
Person
函数声明