【问题标题】:javascript object wont print in consolejavascript 对象不会在控制台中打印
【发布时间】:2017-07-27 17:04:05
【问题描述】:

我有一个带有输入字段的表单。我正在尝试在控制台中记录这些字段。控制台显示“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);

【问题讨论】:

  • 什么是 firstName.text ?
  • 不是您的问题,但拥有一个与您的函数同名的变量并不是最好的主意。在这种情况下,这无关紧要,但如果您需要递归函数,这样做会给您带来一些有趣的错误。
  • 更正这一行 var user = new createUser(document.getElementById('firstName').value);

标签: javascript html object


【解决方案1】:
 var user = new createUser(document.getElementById('firstName').value);

这可能会有所帮助。

【讨论】:

    【解决方案2】:

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

    试试这个

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

    【讨论】:

      【解决方案3】:

      试试这个

      var user = '';
      function createUser(firstName, lastName, email, userName, password ){
      this.firstName = firstName;
      }
      
      function user(){
      user = new createUser(document.getElementById(firstName).value);
      //the same as accessing the text of <input value="this is my first name.">
      
      console.log(user.firstName);
      //log the property firstName of the object user. when you put this.firstName inside a function, it turns that into a property of of the variable you save it as. That means you must call it by using the variable and a "." and the property name ex firstName
      }
      
      console.log(user.firstName);
      

      【讨论】:

        【解决方案4】:

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

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

        <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);
        });
        
        }
        

        【讨论】:

          猜你喜欢
          • 2022-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多