根据同一行中另一个文本字段中的值更改文本字段的值

时间:2015-10-26 10:08:13

标签: jquery yii

我正在使用具有文本字段作为单元格的Yii CGridView。我使用CHtml :: textField来显示文本字段。现在我想在同一行中另一个单元格中更改值时更新网格中的单元格。同一行中的文本字段的ID具有公共编号。例如ProjectMBookGrid_length_163和ProjectMBookGrid_quantity_163。在更改ProjectMBookGrid_length_163值时,我想更改ProjectMBookGrid_quantity_163中的值,在更改ProjectMBookGrid_length_164值时,我想更改ProjectMBookGrid_quantity_164中的值。  JS Fiddle在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>

1 个答案:

答案 0 :(得分:1)

请看一下更新的小提琴:

http://jsfiddle.net/nzdhs4wq/

$(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);
        });

});