首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在除以php变量的另一个输入字段中显示按键时输入字段的值

在除以php变量的另一个输入字段中显示按键时输入字段的值
EN

Stack Overflow用户
提问于 2018-06-27 02:57:21
回答 1查看 156关注 0票数 0

我正在尝试将#output值显示到另一个输入字段中,到目前为止我发现了这个-> http://jsbin.com/oleto5/5/edit?html,js,output

在这里您可以看到何时键入输入字段,显示输入的数据进入< div> which 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,我们应该会得到一个满分为82.644的简单单词"10000/121 = 82.644“

EN

回答 1

Stack Overflow用户

发布于 2018-06-27 03:07:13

<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="" />

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51049751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档