0

フォーム入力フィールドがあります。

<input style="text-align:right;" type="text" name="DiversionCalc_Diversion_Rate" id="calc_dr" value="0.25%" />

jQuery 1.7.2 を使用して focusout に基づいてフォーマットしようとしています

$('#calc_dr').focusout(function () {
    var value = $.trim($(this).val()).toString();
    if (value.indexOf("0.") === -1) {
        var $t = ("0" + value).toString();
        alert($t);
        $(this).val($t);
    }
    if (value != '' && value.indexOf("%") === -1) {
        $(this).val(value + '%');
    }
});

これはほとんど機能していますが、フィールドに .25 を入力すると、アラートは正しい 0.25 をポップアップ表示しますが、$(this).val.25 しか表示されません。

アラートに表示されているものを表示するにはどうすればよいですか???

4

2 に答える 2

1

グローバル$t変数を作成し (if ループから引き出して)、代わりに代入しますvalue

$('#calc_dr').focusout(function () {
    var value = $.trim($(this).val()).toString();
    var $t = value;
    if (value.indexOf("0.") === -1) {
        $t = ("0" + value).toString();
        alert($t);
        $(this).val($t);
    }
    if ($t != '' && $t.indexOf("%") === -1) {
        $(this).val($t + '%');
    }
});
于 2013-12-11T19:04:09.263 に答える