Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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
Javascript HTML表单货币字段-自动将货币格式设置为2位小数和最大长度_Javascript_Jquery_Html - Fatal编程技术网

Javascript HTML表单货币字段-自动将货币格式设置为2位小数和最大长度

Javascript HTML表单货币字段-自动将货币格式设置为2位小数和最大长度,javascript,jquery,html,Javascript,Jquery,Html,你好 我在表单上有一个存款字段,该字段的货币自动格式化为包含2位小数(即,用户键入2500,该字段显示25.00)。但是,我编写的脚本完全忽略了html中包含的maxlength。在这里见我的朋友。我尝试了各种jQuery选项来强制执行该限制,例如: $('input[name=amount]').attr('maxlength',9); 以下是我在页面上使用的脚本: amountValue = ""; $(function() { $(".mib").unbind().keydown(

你好

我在表单上有一个存款字段,该字段的货币自动格式化为包含2位小数(即,用户键入2500,该字段显示25.00)。但是,我编写的脚本完全忽略了html中包含的maxlength。在这里见我的朋友。我尝试了各种jQuery选项来强制执行该限制,例如:

$('input[name=amount]').attr('maxlength',9);
以下是我在页面上使用的脚本:

amountValue = "";

$(function() {
  $(".mib").unbind().keydown(function(e) {
    //handle backspace key
    if (e.keyCode == 8 && amountValue.length > 0) {
      amountValue = amountValue.slice(0, amountValue.length - 1); //remove last digit
      $(this).val(formatNumber(amountValue));
    } else {
      var key = getKeyValue(e.keyCode);
      if (key) {
        amountValue += key; //add actual digit to the input string
        $(this).val(formatNumber(amountValue)); //format input string and set the input box value to it
      }
    }
    return false;
  });

  function getKeyValue(keyCode) {
    if (keyCode > 57) { //also check for numpad keys
      keyCode -= 48;
    }
    if (keyCode >= 48 && keyCode <= 57) {
      return String.fromCharCode(keyCode);
    }
  }

  function formatNumber(input) {
    if (isNaN(parseFloat(input))) {
      return "0.00"; //if the input is invalid just set the value to 0.00
    }
    var num = parseFloat(input);
    return (num / 100).toFixed(2); //move the decimal up to places return a X.00 format
  }
});
amountValue=”“;
$(函数(){
$(“.mib”).unbind().keydown(函数(e){
//手柄退格键
如果(e.keyCode==8&&amountValue.length>0){
amountValue=amountValue.slice(0,amountValue.length-1);//删除最后一位
$(this).val(formatNumber(amountValue));
}否则{
var key=getKeyValue(如keyCode);
如果(关键){
amountValue+=key;//将实际数字添加到输入字符串中
$(this).val(formatNumber(amountValue));//格式化输入字符串并将输入框值设置为它
}
}
返回false;
});
函数getKeyValue(keyCode){
如果(keyCode>57){//也检查numpad键
keyCode-=48;
}

如果(keyCode>=48&&keyCode看起来您正在尝试进行货币格式化。请尝试以下操作:

var convertStringToUSDFormat = function (value) {
    // Create our number formatter.
    var formatter = new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: 'USD',
        minimumFractionDigits: 2,
    });

    return formatter.format(value);
}
如果仍要使用脚本,请添加以下返回:

if (key) {
    amountValue += key; //add actual digit to the input string
    if(amountValue.length >=10) return;
    $(this).val(formatNumber(amountValue)); //format input string and set the input box value to it
 }

您是否尝试过在html中执行此操作。例如:
我们可以查看您的html吗?您是否尝试在将其发送到某个地方之前对其进行格式化,或者只是尝试验证?如果用户想要键入2500美元,则使用HTMLWhat进行更新?为什么您的
type=“tel”
。有关正确的html,请参见我上面的注释。也可以将值传递给我在下面留下的答案,而不是自定义脚本。脚本的工作方式是,用户可以键入任何数字。如果用户需要2500.00,他们将键入250000,脚本插入小数。type=tel是上一次迭代的保留,因此用户可以/c如果他们选择,键入小数。我的要求更改为自动格式。
if (key) {
    amountValue += key; //add actual digit to the input string
    if(amountValue.length >=10) return;
    $(this).val(formatNumber(amountValue)); //format input string and set the input box value to it
 }