在 JavaScript 类中获取 HTML 输入值

getting html input value inside javascript class

本文关键字:HTML 输入 获取 JavaScript      更新时间:2023-09-26

我是javascript的新手,正在制作BMI计算器。 IM 在 JavaScript 中创建一个类来生成计算。 目前用户可以输入他们的身高(以英尺和英寸为单位)和体重。 当我在类中运行方法时,它一直说this.feet = null,而不是从输入中获取值。 我的JavaScript代码如下。 result() 在我的 HTML 中用于提交按钮。

function calculator(feet,inches,weight) { 
    this.feet = feet.value;
    this.inches = inches.value;
    this.weight = weight.value;
    this.validateInput = function() {
        var errors = [];
        if(isNaN(this.feet) || this.feet < 0) {
            errors.push("Feet");
        }
        else {
            return this.feet
        };
        if(isNaN(this.inches) || this.inches < 0 || this.inches > 11) {
            errors.push("Inches")
        }
        else {
            return this.inches;
        };  
        if(isNaN(this.weight) || this.weight < 0) {
            errors.push("Weight");
        }
        else {
            return this.weight
        }
    };
    this.inchesConverter = function() {
        this.feet = parseFloat(feet);
        this.inches = parseFloat(inches);
        return parseInt(this.feet*12+this.inches);
    };
    this.bmi = function() {
        var height = this.inchesConverter();
        var validater = this.validateInput();
        for(r = 0; r < errors.length; r++){
            if(errors.length > 0) {
                return errors[r] + " must be a valid positive number.";
            }
            else {
                parseFloat((validateWeight * 703) / (Math.pow(height, 2))).toFixed(1);
            }
        }
    };      
};

var getWeight = document.getElementById('txtWeight');
var getFeet = document.getElementById('txtHeightFeet');
var getInches = document.getElementById('txtHeightInches');
var test = new calculator(getFeet, getInches, getWeight);
function result() {
    document.getElementById("lblBMI").innerHTML(test.bmi());
}

与其将feet.value分配给this.feet不如尝试分配feet.getAttribute('value')

如果你控制台.log(英尺)它是否显示正确的 DOM 元素?

不需要用"this"声明变量。

function calculator(paraFeet,paraInches,paraWeight) { 
    var feet = paraFeet.value;
    var inches = paraInches.value;
    var weight = paraWeight.value; 
...

现在,您可以从函数中的每个变量中删除"this."

每个浏览器都有JavaScript控制台,您可以在其上调试代码。我在这里或那里修复它有一些乐趣,但你也应该自己尝试一下。无论如何,这里是:

function calculator(weight, feet, inches) {
  this.weight = weight;
  this.feet = feet;
  this.inches = inches;
  this.validateInput = function() {
    var errors = [];
    if (!this.validNumber(this.weight, 1000)) {
      errors.push("Invalid weight.");
    }
    if (!this.validNumber(this.feet, 10)) {
      errors.push("Invalid hight in feet.");
    }
    if (!this.validNumber(this.inches, 11)) {
      errors.push("Invalid hight in inches.")
    }
    return errors;
  };
  this.validNumber = function(num, max) {
    if (num.length > 0 && !isNaN(num)) {
      var number = parseInt(num);
      return number > 0 && number < max + 1;
    }
    return false;
  }
  this.displayErrors = function(errors) {
    var html = "";
    for (error of errors)
      html += error + "<br/>";
    return html;
  };
  this.inchesConverter = function() {
    var feet = parseInt(this.feet);
    var inches = parseInt(this.inches);
    return feet * 12 + inches;
  };
  this.bmi = function() {
    var errors = this.validateInput();
    if (errors.length > 0) {
      return this.displayErrors(errors);
    }
    var height = this.inchesConverter();
    return parseFloat((this.weight * 703) / (Math.pow(height, 2))).toFixed(1);
  };
};
function result() {
  var getWeight = document.getElementById('txtWeight').value;
  var getFeet = document.getElementById('txtHeightFeet').value;
  var getInches = document.getElementById('txtHeightInches').value;
  var test = new calculator(getWeight, getFeet, getInches);
  document.getElementById("lblBMI").innerHTML = test.bmi();
  return false;
}
<span>
  <label>Weight</label>
  <input id="txtWeight">
</span>
<span>
  <label>HeightFeet</label>
  <input id="txtHeightFeet">
</span>
<span>
  <label>HeightInches</label>
  <input id="txtHeightInches">
</span> 
<button id='run' onClick="return result();">Calculate</button>
<div id="lblBMI"></div>

弄清楚

我的问题是什么。问题出在我的 for 循环中。工作代码发布在下面!

function calculator(feet,inches,weight) { 
    this.feet = feet.value;
    this.inches = inches.value;
    this.weight = weight.value;
    this.validateInput = function() {
        var errors = [];
        if(isNaN(this.feet) || this.feet < 0 || this.feet == "") {
            errors.push("feet");
        };
        if(isNaN(this.inches) || this.inches < 0 || this.inches > 11) {
            errors.push("inches")
        };
        if(isNaN(this.weight) || this.weight < 0 || this.weight == "") {
            errors.push("weight");
        };
        return errors;
        console.log(errors);
    }; 
    this.inchesConverter = function() {
        var parseFeet = parseFloat(this.feet);
        var parseInches = parseFloat(this.inches);
        return parseInt(parseFeet*12+parseInches);
    };
    this.bmi = function() {
        var height = this.inchesConverter();
        var errors = this.validateInput();
        if(errors.length > 0) {
            for(r = 0; r < errors.length; r++){
                return "Please enter a correct number for " + errors[r];
            };
        }
        else {
            return parseFloat((this.weight * 703) / (Math.pow(height, 2))).toFixed(1);
        };
    };  
};
function result(){
    var getWeight = document.getElementById('txtWeight');
    var getFeet = document.getElementById('txtHeightFeet');
    var getInches = document.getElementById('txtHeightInches');
    var test = new calculator(getFeet, getInches, getWeight);
    document.getElementById("lblBMI").innerHTML = test.bmi();
};