cuốn sách gpt4 ai đã làm

javascript - 将值格式化为 "doesn' t work'

In lại Tác giả: Walker 123 更新时间:2023-11-28 19:15:24 32 4
mua khóa gpt4 Nike

nhập mô tả hình ảnh ở đây

使用函数格式化值并像这样调用:

$(".vlrTotal").val(formatReal(formatarValor($(".vlrTotal").val())));

我的 HTML:




结果如上图所示。

现在如果我取消格式化该值的功能,结果是这样的: nhập mô tả hình ảnh ở đây

问题是,在格式化值后,所有输入 (".vlrTotal")显示相同的值(第一个),但是当我检查元素时,该值显示正确的值。如果我取消格式化该值的功能,它就会“起作用”。

我不知道该怎么办了。

PS:控制台.log:

input.vlrTotal property value = "7.199,28" attribute value = "2799.72"

input.vlrTotal property value = "7.199,28" attribute value = "2399.76"

câu trả lời hay nhất

由于您有多个带有 $(".vlrTotal") 的元素,因此您需要迭代并为每个元素设置值。当您访问 .val() 方法时,它会按照您的体验获取第一个元素的值。

Bạn cần sử dụng.val( function )

A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old value as arguments.

sử dụng

$(".vlrTotal").val(function(_, value){
return formatReal(formatarValor(value));
});

OR,简单的$.fn.each()

$(".vlrTotal").each(function(){
$(this).val(formatReal(formatarValor($(this).val())));
});

注意:假设您已定义具有所需行为的 formatRealformatarValor chức năng.

关于javascript - 将值格式化为 "doesn' t work',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29969271/

32 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress