2015-10-26 95 views
-3

我正在使用具有文本字段作爲單元格的Yii CGridView。我用CHtml :: textField顯示文本字段。現在我想更新同一行中另一個單元格的值更改中的單元格。同一行中的文本字段的ID具有相同的編號。例如ProjectMBookGrid_length_163和ProjectMBookGrid_quantity_163。在更改ProjectMBookGrid_length_163值時,我想更改ProjectMBookGrid_quantity_163中的值,更改ProjectMBookGrid_length_164值時,我想要更改ProjectMBookGrid_quantity_164中的值。 JS小提琴在https://jsfiddle.net/7ykdkgaw/根據同一行中另一個文本字段中的值更改文本字段的值

<table> 
    <tr><td><input type="text" id="ProjectMBookGrid_length_163" name="ProjectMBookGrid[length_163]" value="5" style="text-align: right" class="gridfield"></td><td><input type="text" id="ProjectMBookGrid_quantity_163" name="ProjectMBookGrid[quantity_163]" value="3" style="text-align: right" class="gridfield"></td></tr> 
    <tr><td><input type="text" id="ProjectMBookGrid_length_162" name="ProjectMBookGrid[length_162]" value="3" style="text-align: right" class="gridfield"></td><td><input type="text" id="ProjectMBookGrid_quantity_162" name="ProjectMBookGrid[quantity_162]" value="3" style="text-align: right" class="gridfield"></td></tr> 
</table> 
+0

請小提琴 –

+0

@ P.Frank加js小提琴 – user1690835

回答

1

請看看更新的小提琴:

https://jsfiddle.net/7ykdkgaw/2/

$(document).ready(function(){ 
    $(document).on("change","input[id^=ProjectMBookGrid_length_]", 
     function(){ 
      var currentVal = $(this).val(); 
      var currentId = $(this).attr("id"); 
      var numberInID = currentId.replace("ProjectMBookGrid_length_",""); 

      var targetText = $("#ProjectMBookGrid_quantity_"+numberInID); 
      targetText.val(currentVal); 
     }); 

}); 
+0

它不應該依賴於「 tr「或其他表格元素。是否可以通過文本字段類和ID? – user1690835

+0

ok ..確實...給我2分鐘...會更新代碼...':)'! – vijayP

+0

更新了答案...! – vijayP

相關問題