简体   繁体   English

我不明白为什么我的代码没有取输入框的值并创建一个新对象

[英]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 : 这是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 + ".";
};

This is the HTML: 这是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>
  • There's no id on the submit button use document.querySelector('input[type="submit"]') ; 提交按钮上没有id使用document.querySelector('input[type="submit"]') ; instead. 代替。
  • The id for that <p> is p1 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的数组中没有任何数据,只有一个,就是这样,我不明白为什么? - My array isn't taking any data in it, only one and that's it, and I don't understand why? 为什么我在JavaScript中看不到字符串的.value属性? - Why don't I see the .value property for my string in javascript? 为什么我的 Javascript 代码没有向 object 添加新属性? - Why isn't my Javascript code adding a new property to an object? 为什么它不读取我的 object 中的输入值? - why isn't it reading the input value in my object? 我的按钮没有从JS React中获取值并没有输入 - My button isn't taking a value from and input in JS React 不明白为什么我的 for 循环代码不起作用 - Don't understand why my for loop code isn't working 我的代码中是否有任何泄漏,只有 1 个案例不起作用我不知道为什么? - Is there any leak in my code, Only 1 case isn't working I don't know why? 为什么进度条不会停留在我放在输入框中的值? - Why Progress bar don't stop at my value placed in the input box? 为什么我在 Stack Navigator 中看不到任何渲染内容? - Why Don't I See Anything Rendering in My Stack Navigator? 为什么我无法在JavaScript代码中获得输入值 - Why I can't get input value in my javascript code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM