2016-10-19 3 views
0

こんにちは皆、私はいくつかの入力ボックスを作成し、いくつかの値を入力するか、入力が自動的に10進数2を追加するための値を取得したい場合jqueryで新しいですしかし、それは親切に動作していないコードは、私がjqueryの入力ボックスで自動小数点を設定する

  ?> 
 
     <script type="text/javascript"> 
 
     function setTwoNumberDecimal(event) { 
 
     this.value = parseFloat(this.value).toFixed(2); 
 
      } 
 
     </script> 
 
     <?
print '<tr><td>'; 
 
\t print fieldLabel('Others Deductions','other_deduction',1).'</td><td>'; 
 
\t print '<input class="sub" onchange="setTwoNumberDecimal(this)" name="other_deduction" id="other_deduction" size="10" value="'.GETPOST("other_deduction").'">'; 
 
\t print '</td></tr>';

答えて

1

次のコードスニペット

$(document).ready(function(){ 
 
    $("#text1").bind('change',function(){ 
 
    var value=$(this).val(); 
 
    $(this).val(parseFloat(value).toFixed(2)); 
 
    }) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="text1" />
をチェック助けます

希望はこれではJavaScriptを使用してマークアップを混在決して

いくつかの提案

  1. に役立ちます。これは

    を助け希望/パラメータとして、あなたのsetValueメソッドのコードでJavaScriptエンド
  2. でクリックして通過イベントを変更し、this.valueにアクセスしようと、ここでこれはwindowオブジェクトに

を参照することになり、あなたのイベントを結合してみてください

+0

プラス入力ボックスに私のデータもJSONとjQuery –

+0

私のコードを使用してデータベースから来てより多くのことを一つのこと私はちょうど古いfjqueryの体の機能を入れて働いて、それは愚かな間違いだった –

0

何も問題ありません。これに代わって、関数内のイベントを使用してください。

Testtingの目的の純htmlコードとあなたのコード:

function setTwoNumberDecimal(event) { 
 
    event.value = parseFloat(event.value).toFixed(2); 
 
    }
<input type="text class="sub" onchange="setTwoNumberDecimal(this)" name="other_deduction" id="other_deduction" size="10" value="">

<script type="text/javascript"> 
 
    function setTwoNumberDecimal(event) { 
 
    event.value = parseFloat(event.value).toFixed(2); 
 
    } 
 
    </script>
<?php print '<tr><td>'; 
 
\t print fieldLabel('Others Deductions','other_deduction',1).'</td><td>'; 
 
\t print '<input class="sub" onchange="setTwoNumberDecimal(this)" name="other_deduction" id="other_deduction" size="10" value="'.GETPOST("other_deduction").'">'; 
 
\t print '</td></tr>'; 
 
?>

0

あなたの問題は、あなたが渡しているありますあなたのjavascript関数へのオブジェクト(正しい)ですが、あなたのように使用していません。

コードは次のようになります。それが動作しない

 function setTwoNumberDecimal(obj) { 
 
     obj.value = parseFloat(obj.value).toFixed(2); 
 
      }
<tr> 
 
    <td>Others Deductions</td> 
 
    <td> 
 
<input type="text" class="sub" onchange="setTwoNumberDecimal(this)" name="other_deduction" id="other_deduction" size="10" value="" /> 
 
    </td> 
 
</tr>
まだ

関連する問題