我如何在数字插入中使用逗号,这样它就不会'不要破坏我的输入字段

How can I use commas in number inserts so it doesn't break my input fields

本文关键字:输入 字段 我的 数字 插入      更新时间:2023-09-26

我创建了一个计算表单,并用它进行了一些a/B测试,我注意到在一个领域中,许多要求"总捐赠者"的人会尝试使用"10,000"而不是10000

我有没有办法让逗号可以放进去,但在JS中被忽略和未读?因此,它向用户显示10,000,但我的脚本仍然将其读取为10000。它必须在多个领域进行,有全球解决方案吗?

我对JS的了解还不够,甚至不知道如何开始攻击这个问题。我可以想象,它必须是某种类型的代码,计算字符串值,然后在里面弹出逗号,这样它就可以作为数字返回。

举个例子,我使用的字段也会根据输入的数字在总分中添加加分。由于计算表将被各种规模的机构使用,我们需要添加它来平衡它,这样拥有更多捐赠者的大公司就可以获得更多的分数,因为小公司的总分既不会受到惩罚。

小公司总分:400

适用奖金的公司的总分:420

奖金总额:

• val < 10001 = + 0
• val >= 10001 and < 25001 = + 10
• val >= 25001 and < 50000 = + 15
• val >= 50000 = + 2

我的输入如下(为了可读性而拆分):

<fieldset class="webform-component-fieldset form-wrapper" id="webform-component-constituent-base">
    <legend><span class="fieldset-legend">Constituent Base</span></legend>
    <div class="fieldset-wrapper"><div class="form-item webform-component webform-component-textfield" id="webform-component-constituent-base--total-constituents">
    <label for="edit-submitted-constituent-base-total-constituents">Total Number of constituents in your database </label>
    <input type="text" id="edit-submitted-constituent-base-total-constituents" name="submitted[constituent_base][total_constituents]" value="" size="60" maxlength="128" class="form-text" />
    </div>
    </div></fieldset>

我对此字段的计算

var donorTotal = jQuery("#edit-submitted-constituent-base-total-constituents").val();
    if(donorTotal)
    {
        donorTotal = parseFloat(donorTotal);
    }
    else
    {
        donorTotal = 0;
    }
    grade += getBonusDonorPoints(donorTotal);
    // End total # of donors

积分功能:

function getBonusDonorPoints(val)
{
    if(val < 10001)
    {
        return 0;
    }
    else if(val >= 10001 && val < 25001)
    {
        return 10;
    }
    else if(val >= 25001 && val < 50000)
    {
        return 15;
    }
    else if(val >= 50000)
    {
        return 20;
    }
}

谢谢你们能给我的任何帮助,让我指向并回答!

因此,请替换逗号,并确保使用的是数字而不是字符串。

Number(jQuery("#edit-submitted-constituent-base-total-constituents").val().replace(/,/g,""));

我会通过删除任何不是数字的东西来解决这个问题。这对诸如SSN、电话号码、邮政编码之类的字段非常有效,并且有利于这些字段的国际化。

function justTheNumbers(s) {
  return s.replace(/'D/g, '');
}