简体   繁体   中英

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

I am trying to display #output value into another input field so far I found this -> http://jsbin.com/oleto5/5/edit?html,js,output
here you can see when type into input field showing entered data into < div> which id=output <input id="txt" type="text" />

what I am looking to achieve:
1 - Changes in html- I am looking to show #output as a value into another input field like this <input id="output" type="text" />
2 - Changes in script - I also want to do changes in calculation i have php variable named $final_rate for, i want to "Output" deivide by php variable $final_rate

Expected Code Example

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

in above example if we enter 10000 in #txt input field we should get an out of 82.644 in simple words "10000/121 = 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="" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM