2017-02-10 69 views
1

概念是我將創建該div與其div的ID,輸入的ID和名稱的增量。百分比字段應該能夠檢測輸入是否正在改變。我該如何做這項工作?它似乎只適用於創建的第一個輸入窗口小部件。檢測某些輸入是否有變化

<div id="divRdTest"> 
    <div class="form-group" id="rdTestFld1"> 
     <div class="col-sm-7 col-sm-offset-2"> 
       <center> 
        <input type="text" id="txtRTName1" name="txtRTName1" class="form-control"> 
       </center> 
     </div> 
    <div class="col-sm-3"> 
     <center> 
      <input type="text" id="txtRTPercent1" name="txtRTPercent1" class="form-control percent-compute"> 
      </center> 
      </div> 
     </div> 
</div> 


//The script 

$(".percent-compute").on("input", function(e){ 
    //computation code 
}); 
+0

也許嘗試,'$。在( 「百分比,計算」。)( 「改變」,函數(E){});' –

回答

0

如果你的DIV是動態創建的,你必須使用document選擇上應用input, change.

$(document).on("input change",".percent-compute", function(e){ 
 
    console.log($(this).val()) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="divRdTest"> 
 
    <div class="form-group" id="rdTestFld1"> 
 
     <div class="col-sm-7 col-sm-offset-2"> 
 
       <center> 
 
        <input type="text" id="txtRTName1" name="txtRTName1" class="form-control"> 
 
       </center> 
 
     </div> 
 
    <div class="col-sm-3"> 
 
     <center> 
 
      <input type="text" id="txtRTPercent1" name="txtRTPercent1" class="form-control percent-compute"> 
 
      </center> 
 
      </div> 
 
     </div> 
 
</div>