0

javascript と html を使用して小さなアプリを作成しました。

しかし最近、スクリプトで問題が発生しました。

問題 :入力テキスト ボックスで、(.)ドット/ピリオド、つまり 10 進数値を入力できません。例: 10 進数の値を入力すると、入力テキスト ボックスから自動的に削除されます。

この理由は何でしょうか。

過去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

あなたの問題は、変換関数がキーアップでトリガーされるため、ユーザーが入力3.すると変換さ3れてフィールドに戻されることです。

parseFloat(3.) = 3

この問題を解決するにはいくつかの方法があります。

keyup最も簡単な方法は、ユーザーがヒットしたとき、enterまたはフィールドがフォーカスを失ったときに値を計算しないことです( blur)。

他のオプションは、フィールドを解析する前にフィールドの文字列値をチェックし、.有効な数値でない限り変更しないことです。

例:

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 に答える