代码在
这是 Javascript:
document.getElementById("submit").onclick = function() {
function Person(fullname, job, country, city) {
this.fullname = fullname;
this.job = job;
this.country = country;
this.city = city;
}
var fullname = document.getElementById("fname").value;
var job = document.getElementById("job").value;
var country = document.getElementById("country").value;
var city = document.getElementById("city").value;
var me = new Person(fullname, job, country, city);
document.getElementById("p").innerHTML = me.fullname + ". Congratulations an object has been made with your information stored in it, the following information is what you have given us. Your job is a " + me.job + "You live in" + me.city + "," + me.country + ".";
};
最佳答案
- 提交按钮上没有 id 使用
document.querySelector('input[type="submit"]')
;相反。 <p>
的 ID是p1
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <div> <input type="text" id="fname" placeholder ="Full Name" name="fullname"> <input type="text" id="job" placeholder ="Job" name="job"> <input type="text" id="country" placeholder ="Country" name="country"> <input type="text" id="city" placeholder ="City" name="city"> <input type="submit" value="Submit"> <p id="p1"></p> </div> <script> document.querySelector('input[type="submit"]').onclick = function() { function Person(fullname, job, country, city) { this.fullname = fullname; this.job = job; this.country = country; this.city = city; } var fullname = document.getElementById("fname").value; var job = document.getElementById("job").value; var country = document.getElementById("country").value; var city = document.getElementById("city").value; var me = new Person(fullname, job, country, city); document.getElementById("p1").innerHTML = me.fullname + ". Congratulations an object has been made with your information stored in it, the following information is what you have given us. Your job is a " + me.job + "You live in" + me.city + "," + me.country + "."; }; </script> </body> </html>
关于javascript - 我不明白为什么我的代码没有获取输入框的值并创建一个新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35421481/