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

标签 javascript jquery calculator date-arithmetic

我创建了一个计算表格,并用它运行了一些 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,""));

关于javascript - 如何在数字插入中使用逗号,这样就不会破坏我的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29539990/

相关文章:

iphone - 如何在 Objective-C 中进行十进制格式化?

javascript - 如何将自定义事件添加到 svg.js

javascript - 从 Node.js 返回 URL 时出现 "Error: Capture"

javascript - 如何在不影响其余子元素的情况下只粘一个子元素?

javascript - clearInterval() 在 iPad 上不起作用

javascript - 我的增值税计算错误

javascript - jQuery 无冲突包装器不工作

javascript - 无法读取谷歌图表中未定义的属性 'count'

javascript - 固定列表的固定标题根据宽度不响应

c++ - 使用二次公式的简单二次方程计算器