当我输入印度卢比格式附带的数字并且结果也以印度卢比格式粘贴在总输入框上时,如何使它生成? 请帮忙。
$admin_price = (int)get_post_meta(get_the_ID(), '_admin_price ', true);
// Loop through cart items
foreach ( $cart_obj->get_cart() as $cart_item ) {
$cart_item['data']->set_price($admin_price);//don't work
$cart_item['data']->set_price(50);//working
}
这里是javascript
<input onblur="findTotal()" type="text" name="qty" id="qty1"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty2"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty3"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty4"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty5"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty6"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty7"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty8"/><br>
<br><br>
Total : <input type="text" name="total" id="total"/>
答案 0 :(得分:1)
您可以使用Intl.NumberFormat
方法为您处理所有事情,我在下面创建了演示。
与现有代码相似,不同之处在于它首先将值传递给数字格式器。
var formatter = new Intl.NumberFormat('en-IN', {
style: 'currency',
currency: 'INR',
minimumFractionDigits: 2,
});
//formatter.format(2500); /* $2,500.00 */
function findTotal(){
var arr = document.getElementsByName('qty');
var tot = 0;
for(var i=0;i<arr.length;i++){
if(parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
document.getElementById('total').value = formatter.format(tot);
}
<input onkeyup="findTotal()" type="text" name="qty" id="qty1"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty2"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty3"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty4"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty5"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty6"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty7"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty8"/><br>
Total : <input type="text" name="total" id="total"/>
有关浏览器的支持,请参阅: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat#Browser_compatibility
答案 1 :(得分:1)
您可以执行以下操作以获取INR货币格式的字符串:
function formatINR(x){
return x.toLocaleString('en-IN');
}
var testString = 123342;
alert(formatINR(testString));
答案 2 :(得分:0)
function toIndianRs($number ) {
return (isNaN(parseInt($number))) ? 0 : /*'₹' + */ parseInt($number).toLocaleString('en-IN')
}
答案 3 :(得分:-1)
正如已经显示的那样(我已经开始这样做了...),Intl.NumberFormat
方法在处理各种形式的数字时具有很大的灵活性。下面的重写演示演示了如何使用它,并删除了可怕的内联函数调用'-)
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Indian Rupees</title>
<style>
form{
width:50%;float:none;margin:2rem auto;padding:1rem;box-sizing:border-box;
}
form > input{
width:100%;padding:1rem;margin:0.25rem;box-sizing:border-box;
}
output{
display:block;width:50%;padding:1rem;box-sizing:border-box;float:none;margin:1rem auto;text-align:center;
}
</style>
<script>
/*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
*/
const formatcurrency=function(i){
return new Intl.NumberFormat( 'hi-IN', { style:'currency', currency: 'INR' } ).format( parseFloat( i ) );
};
document.addEventListener('DOMContentLoaded',function(e){
let out=document.querySelector('output');
let col=document.querySelectorAll( 'form > input[name="qty[]"]' );
col.forEach( input => {
input.addEventListener( 'blur', function(e){
let total=0;
col.forEach( function(e){
if( e.value ) total += parseFloat( e.value )
});
out.innerText=formatcurrency( total );
}, false );
} )
},false);
</script>
</head>
<body>
<output></output>
<form>
<input type='number' name='qty[]' />
<input type='number' name='qty[]' />
<input type='number' name='qty[]' />
<input type='number' name='qty[]' />
<input type='number' name='qty[]' />
<input type='number' name='qty[]' />
<input type='number' name='qty[]' />
<input type='number' name='qty[]' />
</form>
</body>
</html>