当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - I don't see why my code isn't taking the value of the input box and making a new object

问题描述:

The code for this is at

This is the 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 + ".";

};

网友答案:
  • There's no id on the submit button use document.querySelector('input[type="submit"]'); instead.
  • The id for that <p> is 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>
网友答案:

This is the HTML:

<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>
</body>
分享给朋友:
您可能感兴趣的文章:
随机阅读: