我有脚本工作,将根据需要更改我的文本框的值。我正在尝试更多地改变它。
var total = 0; // adds a decimal and 2 zeros
$('input:checkbox:checked').each(function(){
total += isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val());
});
$("#total").val(total.toFixed(2));$.fn.digits = function(){ // adds a comma every 3 numbers
return this.each(function(){
$(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );
})
}
$("#total").digits();<input type="text" class="form-control" id="total" style="background-color:#ffffff;"><span id="total_text"></span>$("#total_text").digits(); // this works to change the span tag.toFixed(2)可以很好地更改文本框和跨度标记。
当我尝试添加.digits()时,文本框没有任何变化。
发布于 2019-04-21 20:44:04
您需要使用.val()而不是.text()来读取和更改textbox的值。因此该函数需要检查$(this)是哪种类型的元素。
$.fn.digits = function(){ // adds a comma every 3 numbers
return this.each(function(){
var method = $(this).is(":input") ? "val" : "text";
$(this)[method]( $(this)[method]().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );
})
}
$("#total").digits();https://stackoverflow.com/questions/55782436
复制相似问题