我的页面上有 2 个文本框和一个标签。 2 个文本框将包含数值。标签文本将是 2 个文本框值的乘积。有没有办法使用 JQuery 来执行此操作,以便在编辑文本框时可以更新该值,而无需进行回发?
文本框也可能包含带逗号的值:例如10,000。有没有办法可以从中提取数字,以便可以用它来计算标签值。
提前致谢,
Zap
最佳答案
我还无法对其他答案添加评论,因此我将在此处发布更新。
原来的问题涉及乘积,这意味着乘法,所以这里有一个允许无限文本框并完成乘法的版本。
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);
});
});
关于asp.net - JQuery 更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2942901/