Multiply input value on keyup
How do I multiple the value of one input by 2 and save it to another input?
So when I type 10 (for example), the value of the 2nd input will be 20?
Than开发者_运维问答ks
Two input fields
<input type="text" id="input-value" />
<input type="text" id="output-value" readonly />
And some simple jQuery
var $output = $("#output-value");
$("#input-value").keyup(function() {
var value = parseFloat($(this).val());
$output.val(value*2);
});
Assuming a structure like this:
<input id="inputfield1" type="text" />
<input id="inputfield2" type="text" />
you could use this code:
$("#inputfield1").keyup(function() { // when key is released in "#inputfield1"
// "change()" is also possible instead of "keyup()", slightly different behavior
var input_value = parseFloat($("#inputfield1").val()); // get number as float
// alternately parseInt(string, 10), in case you work with integers
if (!isNaN(input_value)) { // the input is a number
$("#inputfield2").val(input_value * 2); // update second field
} else { // the input wasn't a number
$("#inputfield2").val("not a number?"); // show an error mesage
}
});
I think this is what you are looking for:
$('#input-2').val($('#input-1').val() * 2);
var orgNo = $('#textInput').val();
var newNo = org * 2;
$('#textOutput').val(newNo);
精彩评论