在 JavaScript 类中获取 HTML 输入值
getting html input value inside javascript class
我是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();
};
相关文章:
- 如何编写HTML输入的JS内联
- 如何在HTML输入字段中添加不可删除的后缀
- HTML输入中不可更改的前导字符
- 如何使用jQueryAjax使用动态html输入texbox提交表单
- 清理HTML输入值
- 如何使用jQuery动态创建HTML输入
- 通过javascript以编程方式将文件插入HTML输入
- jQuery选择器不识别任何动态创建的HTML输入函数
- 将JavaScript变量传递到HTML输入框和(在PHP文件中使用)
- 如何调用两个函数是一种html输入类型
- HTML输入框未触发事件
- 移动HTML输入文本框在提交时隐藏软键盘
- 按键事件上的简单HTML输入不起作用
- 选择同一文件时未触发HTML输入文件选择事件
- 如何使文本、数字和日期html输入字段以一致的方式支持Ctrl+Z(撤消)
- 更改 html 输入字段中的占位符属性
- 如何在不使用任何 html 输入/搜索元素的情况下读取本地客户端文本文件
- 持久性 HTML 输入占位符
- 将 html 输入转换为正确的编码
- 如何隐藏和显示 html 输入文本和段落