0

我使用 javascript 和 html 制作了一个小应用程序。

但最近我的脚本遇到了问题。

问题:输入文本框不允许我输入(.)点/句点,即十进制值。示例:如果我输入一个十进制值,那么它会自动从我的输入文本框中删除。

这可能是什么原因。

我正在尝试从过去 3 小时解决这个问题。但找不到确切的问题。请检查我的代码并帮助我解决这个问题。

相关代码:

<table border="0">
    <form name="form_A" onsubmit="return false">
        <tr>
            <td>
                <select name="unit_menu" class="Items" onchange="CalculateUnit(this.form, document.form_B)">
                    <option>
                    ------- Select a Property -------
                                                                    <option>
                    <option>
                    <option>
                    <option>
                    <option>
                </select>

            </td>
            <td>
                <input type="text" id="info" name="unit_input" placeholder="Type to convert" class="textbox" onfocus="CalculateUnit(this.form, document.form_B)" onkeyup="CalculateUnit(this.form, document.form_B)"></td>

        </tr>
    </form>
    <form name="form_B" onsubmit="return false">
        <tr>

            <td>
                <select name="unit_menu" class="Items" onchange="valuefocus()">
                    <option>
                    ------- Select a Property -------
                    <option>
                    <option>
                    <option>
                    <option>
                    <option>
                </select></td>
            <td>
                <input type="text" name="unit_input" size="20" maxlength="20" placeholder="Type to convert" class="textbox" onfocus="CalculateUnit(this.form, document.form_A)" onkeyup="CalculateUnit(this.form, document.form_A)"></td>
            <td></td>
        </tr>
    </form>
</table>

Javascript

         function CalculateUnit(sourceForm, targetForm){
            // A simple wrapper function to validate input before making the conversion
            var sourceValue = sourceForm.unit_input.value;

            // First check if the user has given numbers or anything that can be made to
            // one...
            sourceValue = parseFloat(sourceValue);
            if ( !isNaN(sourceValue) || sourceValue == 0){
                // If we can make a valid floating-point number, put it in the
                // text box and convert!
                sourceForm.unit_input.value = sourceValue;
                ConvertFromTo(sourceForm, targetForm);
            } else {
                 //wrong input
            }
        }
4

1 回答 1

1

您的问题是您的转换功能是在 keyup 上触发的,因此如果用户键入3.它会被转换3并放回该字段。

parseFloat(3.) = 3

有几种方法可以解决这个问题。

最简单的方法是不计算值,keyup但当用户点击enter或字段失去焦点时(blur)。

其他选项是在解析之前检查字段的字符串值是否以 a 结尾,.并保持不变,只要它不是有效数字。

例子:

function CalculateUnit(sourceForm, targetForm){

    // A simple wrapper function to validate input before making the conversion
    var sourceValue = sourceForm.unit_input.value;

    // First check if the user has given numbers or anything that can be made to
    // one...
    if (!/\.$/.test(sourceValue)) {
        sourceValue = parseFloat(sourceValue);
        if ( !isNaN(sourceValue) || sourceValue == 0){
            // If we can make a valid floating-point number, put it in the
            // text box and convert!
            sourceForm.unit_input.value = sourceValue;
            ConvertFromTo(sourceForm, targetForm);
        } else {
             //wrong input
        }
    } else {
        // sourceValue ends with a '.'
    }
}
于 2013-01-07T16:01:54.293 回答