Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在文本输入字段中将值设置为货币_Jquery_Html_Input_Currency - Fatal编程技术网

Jquery 在文本输入字段中将值设置为货币

Jquery 在文本输入字段中将值设置为货币,jquery,html,input,currency,Jquery,Html,Input,Currency,如何将值显示为带有美元符号和逗号的货币 <input type="text" value="4435800" id="cost"> 有什么建议吗?您应该将“text”改为“val”,以更改html元素的值 $.fn.digits = function(){ return this.each(function(){ $(this).val( $(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); })

如何将值显示为带有美元符号和逗号的货币

<input type="text" value="4435800" id="cost">
有什么建议吗?

您应该将“text”改为“val”,以更改html元素的值

$.fn.digits = function(){ 
 return this.each(function(){ 
    $(this).val( $(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
 })
}

正则表达式,您是否应该使用
.val()
或其他东西而不是
.text()
?我更新了OP的可能副本,以显示我将其更新为“val”时发生的情况。它实际上并没有改变值,只是将新格式化的数字放在输入字段之间。Nvm错过了第2个.val()。谢谢
$.fn.digits = function(){ 
 return this.each(function(){ 
    $(this).val( $(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
 })
}