douyan8413 2016-06-04 06:09
浏览 31
已采纳

从jQuery中检测PHP foreach中的输入值更改

I have a PHP foreach loop inside a form where the value comes from database. Here, the input ID increases with the element of the loop.

<form class="form-horizontal">
    <?php
        $count = 0;
        foreach ($value as $row) {
            $count += 1;
            echo 
            '<div class="row">
                <div class="form-group">
                    <label id="durationLabel' . $count . '">Duration (days)</label>
                    <input type="number" name="duration_days" value="' . $row['duration_days'] . ' day" class="form-control input-sm" id="durationInput' . $count . '" placeholder="Days" onchange="durationInputChange(this.value,'.$count.')">
                 </div>             
             </div>';
         }
     ?>
</form>

Now, my question is, how can I detect if any input value is changed (based on ID) and also get that value and show that in that corresponding Label using jQuery. The current JavaScript code I have doesn't do anything on value change:

function durationInputChange(a,i) { 
    var d = document.getElementById("durationInput"+i).value; 
    alert(d);
}
  • 写回答

3条回答 默认 最新

  • donglian4879 2016-06-04 06:22
    关注

    You cannot use change merely if you want more and faster feedback. The change is event is executed after blurring the input element. You can add keyup, keydown, and more like paste. Furthermore, you don't need the onchange attribute--no for inline binding and event listeners.

    $(function(){
      
      $('input[name="input-1"]').on('keyup change paste keydown', function(){
        
        var $this = $(this); 
        
        console.log($this.attr('id')); // ID; for referencing 
        console.log($this.val()); // value of this input
    
      });
    
    })
    input {
      display:block;
      margin-bottom:10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
    <input name="input-1" value="" id="input-1">
    <input name="input-1" value="" id="input-2">
    <input name="input-1" value="" id="input-3">
    <input name="input-1" value="" id="input-4">
    <input name="input-1" value="" id="input-5">
    <input name="input-1" value="" id="input-6">
    <input name="input-1" value="" id="input-7">
    <input name="input-1" value="" id="input-8">
    <input name="input-1" value="" id="input-9">
    <input name="input-1" value="" id="input-10">
    <input name="input-1" value="" id="input-11">
    <input name="input-1" value="" id="input-12">
    <input name="input-1" value="" id="input-13">

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题