jQuery 如何在不单击按钮的情况下将两个值相乘 - javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19699642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to multiply two values without clicking a button - javascript
提问by razor
Here is my fiddle
这是我的小提琴
<input type="text" name="input1" id="input1" value="5">
<input type="text" name="input2" id="input2" value="">
<a href="javascript: void(0)" onClick="calc()">Calculate</a>
<input type="text" name="output" id="output" value="">
Javascript
Javascript
function calc(){
var textValue1 = document.getElementById('input1').value;
var textValue2 = document.getElementById('input2').value;
document.getElementById('output').value = textValue1 * textValue2;
}
Here everything working fine when click on calculate link, it multiplies both the text input numbers and shows the result in the third box but I need it to automatic multiplication with the link calculate.
单击计算链接时,这里一切正常,它将文本输入数字相乘并在第三个框中显示结果,但我需要它与链接计算自动相乘。
I mean user can enter a value in the 2nd text box so it automatically multplies and shows the result in the 3rd textbox without any button.
我的意思是用户可以在第二个文本框中输入一个值,这样它就会自动乘以并在没有任何按钮的第三个文本框中显示结果。
回答by SarathSprakash
回答by Tushar Gupta - curioustushar
Use onkeyup="calc()"
on input
elements
onkeyup="calc()"
在input
元素上使用
<input type="text" name="input1" id="input1" onkeyup="calc()"value="5">
<input type="text" name="input2" id="input2" onkeyup="calc()" value="">
回答by CodeWalker
Try this!
尝试这个!
function add() {
var x = parseInt(document.getElementById("a").value);
var y = parseInt(document.getElementById("b").value)
document.getElementById("c").value = x * y;
}
Enter 1st Number :
<input type="text" id="a">
<br>
<br>Enter 2nd Number :
<input type="text" id="b" onkeyup="add()">
<br>
<br>Result :
<input type="text" id="c">
回答by Pragnesh Chauhan
you can call calc()
function on blur
event
你可以calc()
在blur
事件上调用函数
function calc(){
var textValue1 = document.getElementById('input1').value;
var textValue2 = document.getElementById('input2').value;
if($.trim(textValue1) != '' && $.trim(textValue2) != ''){
document.getElementById('output').value = textValue1 * textValue2;
}
}
$(function(){
$('#input1, #input2').blur(calc);
});
回答by bipen
using jquery..
使用 jquery..
try this
尝试这个
$('#input1,#input2').keyup(function(){
var textValue1 =$('#input1').val();
var textValue2 = $('#input2').val();
$('#output').val(textValue1 * textValue2);
});
回答by Farmer Joe
try this:
尝试这个:
<input type="text" name="input2" id="input2" value="" onchange="calc()">
You may want to add a check for a blank value, etc.
您可能想要添加一个空白值的检查等。