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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。