javascript对象不会在控制台中打印

时间:2017-07-27 17:04:05

标签: javascript html object

我有一个带输入字段的表单。我试图在控制台中记录这些字段。控制台显示“createUser {firstName:null}” : 对象

<div class="#">
    First Name:<br>
    <input placeholder="First Name" type="text" id="firstName" required> 
    <br>
</div>
<button onclick="user()" class="btn btn-primary">Sign Up</button>


function createUser(firstName, lastName, email, userName, password ){
    this.firstName = firstName;
}

function user(){
    var user = new createUser(document.getElementById(firstName.text));
    console.log(user);
}

console.log(user);

4 个答案:

答案 0 :(得分:2)

 var user = new createUser(document.getElementById('firstName').value);

这可能会有所帮助。

答案 1 :(得分:0)

您正在定义与函数名称相同的对象名称。你需要改变一个或另一个。你还需要获得输入字段的值。

试试这个

var userObj = new createUser(document.getElementById('firstName').value);
console.log(userObj)

答案 2 :(得分:0)

试试这个

Rails.application.config.eager_load_paths
=> ["/app/assets",
 "/app/channels",
 "/app/controllers",
 "/app/controllers/concerns",
 "/app/helpers",
 "/app/jobs",
 "/app/mailers",
 "/app/models",
 "/app/models/concerns"]

答案 3 :(得分:0)

您可以通过以下方式在控制台日志中打印所有输入字段值:

考虑到,您有以下输入字段:

<div class="divclass">
    First Name:<br>
    <input placeholder="First Name" type="text" id="firstName" required> 
    <br>
    Middle Name:<br>
    <input placeholder="Middle Name" type="text" id="middleName" required> 
    <br>
    Last Name:<br>
    <input placeholder="Last Name" type="text" id="lastName" required> 
    <br>
</div>
<button onclick="user()" class="btn btn-primary">Sign Up</button>

function user(){
    var inputFields = document.querySelectorAll('#firstName, #middleName, #lastName');
    var inputFieldValues = Array.prototype.forEach.call(inputFields, function(inputField){
         console.log("id: "+inputFields.id + " value: "+inputField.value);
});

}