简体   繁体   English

如何获取2个数字输入字段的值,然后使用Jquery进行除法并将结果显示给另一个输入?

[英]How do i take values of 2 input fields which are numbers, then divide using Jquery and display the result to another input?

I have two input fields 我有两个输入字段

<label>WEIGHT</label>
<input type="number" name="weight" id="weight" class="form-control" placeholder="weight in KG">
<label>HEIGHT</label>
<input type="number" name="height" id="height" class="form-control" placeholder="Height in Meters">

I want to take the value of weight and divide it by the height then assign the result to this input field in realtime using JQuery 我想将权重值除以高度,然后使用JQuery将结果实时分配给此输入字段

<label>BMI</label>
<input type="text" name="bmi" id="bmi" class="form-control" placeholder="weight/height">

I have tried this but its not working 我已经尝试过了,但是没有用

$(document).ready(function(){
   var weight = $("#weight").val();
   var height = $("#height").val();
   var bmi = weight/height;
   $("#bmi").val() = bmi;
});

When you use jQuery's .val to set a value, you need to pass the new value inside the parantheses, ie: .val(newValue) . 当您使用jQuery的.val 设置值时,您需要在括号内传递新值,即: .val(newValue)

So you code should be like this: 因此,您的代码应如下所示:

$("#bmi").val(bmi);

Also, there's no sense in doing these calculations on document.ready , since the inputs will have no value. 另外,对document.ready进行这些计算也没有意义,因为输入将没有任何价值。 You need to move the code to a change event: 您需要将代码移至change事件:

$("#height").change(function(){
   var weight = $("#weight").val();
   var height = $("#height").val();
   var bmi = weight/height;
   $("#bmi").val(bmi);
});

use parseInt like this: 像这样使用parseInt

$("#bmi").click(function (){
    var weight = parseInt($("#weight").val());
    var height = parseInt($("#height").val());
    var bmi = weight/height;
    $("#bmi").val(bmi);
}

暂无
暂无

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

相关问题 您如何添加输入字段中的值并使用jQuery中的结果更新另一个字段? - How do you add the values from input fields and update another field with the result in jQuery? 如何使用JavaScript向输入字段添加值? - How do I add values to input fields using JavaScript? 如何制作表格(两个输入)将我带到另一页,其选择取决于输入 - How do I make form (two inputs) take me to another page which selections depends on the input 从输入和打印结果中除数 - Divide Numbers from Input and Print Result 在使用 jQuery 在特定结果 div 中单击它后,如何显示所有动态无线电输入值的总和? - How do I show the sum of all dynamic radio input values after I click on it in a specific result div by using jQuery? 如何在 javascript 中使用.textContent 获取用户输入并在屏幕上显示问候语? - How do I take a users input and display a greeting on the screen using .textContent in javascript? 我如何乘以只读输入字段的值并将结果显示在另一个字段中 - How do i Multiply the Value of a readonly input field and display the result in another field 如何继续将输入字段的值添加到另一个标签中 - How do I keep adding the values of my input fields into another tag 如何获取输入字段的值并将其显示在对话框中? - How do I get the value of input fields and display it on the dialog box? 如何使用ng-if显示或隐藏输入字段? - How do I use ng-if to display or hide input fields?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM