簡體   English   中英

在另一個輸入字段中的按鍵上顯示輸入字段的值,該值除以php變量

[英]Display input field's value on keypress in another input field divided by php variable

到目前為止,我試圖將#output值顯示到另一個輸入字段中-> http://jsbin.com/oleto5/5/edit?html,js,output
在這里,您可以看到在輸入字段中鍵入內容時顯示的是輸入到<div>中的數據,其中id = output <input id="txt" type="text" />

我要實現的目標:
1-html中的更改-我希望將#output作為值顯示在另一個輸入字段中,例如<input id="output" type="text" />
2-腳本更改-我也想進行計算更改,我有一個名為$ final_rate的php變量,我想通過php變量$ final_rate進行“輸出”划分

預期代碼示例

<body>
  <input id="txt" type="text" />
  <input id="output" type="text" value=""/>
</body>
<?php $final_rate = "121";?>
   <script>
 $(function(){
      $('#txt').keydown(function(){
        setTimeout(function() {
          $('#output').text($('#txt').val());
        }, 50);
      });
    });
</script>

在上面的示例中,如果我們在#txt輸入字段中輸入10000,則應該用簡單的單詞“ 10000/121 = 82.644”得出82.644

<body>
  <input id="txt" type="text" />
  <input id="output" type="text" value=""/>

  <script>
    //put the value in a javascript variable as a Number
    var finalRate = <?php echo "121"; ?>;

    $(function(){
      //bind on the input event, which happens any time the value of the input changes
      $('#txt').on('input', function(e){
        //console log the rate just for debugging
        console.log(finalRate);
        //console log the math just for debugging
        console.log(parseFloat(e.target.value)/finalRate);
        //turn the value in the input into a Number, and perform the math
        $('#output').val(parseFloat(e.target.value)/finalRate);
      });
    });
  </script>
</body>

 //put the value in a javascript variable as a Number var finalRate = 121;//'<?php echo "121"; ?>; $(function() { //bind on the input event, which happens any time the value of the input changes $('#txt').on('input', function(e) { //console log the rate just for debugging console.log(finalRate); //console log the math just for debugging console.log(parseFloat(e.target.value) / finalRate); //turn the value in the input into a Number, and perform the math $('#output').val(parseFloat(e.target.value) / finalRate); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="txt" type="text" /> <input id="output" type="text" value="" /> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM