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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀