我的页面上有两个文本框和一个标签。这两个文本框将包含数值。标签文本将是两个文本框值的乘积。有没有一种方法可以使用JQuery来实现这一点,以便在我编辑文本框时可以更新该值,而不必执行回发?
此外,文本框中可能包含带逗号的值:例如,10,000。有没有一种方法可以从中提取数字,以便用它来计算标签值。
提前谢谢你,
Zaps
发布于 2010-06-02 18:41:34
我还不能对其他答案添加评论,所以我只会在这里发布更新。
最初的问题涉及到product,这意味着乘法,所以这里有一个版本,允许无限的文本框并完成乘法。
function makeInt(text) {
return parseInt(text.replace(',', ''));
}
$(function(){
//hook all textboxes (could also filter by css class, if desired)
//this function will be called whenever one of the textboxes changes
//you could change this to listen for a button click, etc.
$("input[type=text]").change(function(){
var product = 1;
//loop across all the textboxes, multiplying along the way
$("input[type=text]").each(function() {
product *= makeInt($(this).val());
});
$("#display-control-id").html(product);
});
});
发布于 2010-05-31 10:36:21
$('#SecondTextbox').keyup(function() {
var t1 = $('#FirstTexbox').val();
var t2 = $(this).val();
var result = t1+t2;
$('#resultLabel').html(result);
});
这可以做到这一点,或者你可以让它在一个带有链接元素的点击事件上。这也不会有任何页面刷新。
$('#checkButton').click(function() {
var t1 = $('#FirstTexbox').val();
var t2 = $('#SecondTextbox').val();
var result = t1+t2;
$('#resultLabel').html(result);
});
链接可以是这样的,
<a id="checkButton" title="Check your result">Check</a>
这样你就有了css设置'cursor:pointer;‘,让它看起来像是一个合适的链接。
https://stackoverflow.com/questions/2942901
复制相似问题