使用angularjs获取表中textfield的值

时间:2015-11-18 05:03:39

标签: angularjs

以下是我的angularjs表格

<div ng-app="Myapp">
    <div ng-controller="orderFormController">
        <table  id="item_table">
            <thead>
                <tr class="headings">
                    <th class="column-title">Item </th>            
                    <th class="column-title">Rate</th>                          

                    </th>

                </tr>
            </thead>

            <tbody>
                <tr>
                    <td>         
                        <select name="item"  ng-model="item" >
                            <option value="">Choose..</option>
                            <option value="T-SHIRT">T-SHIRT</option>
                            <option value="JACKET">JACKET</option>

                        </select>
                    </td>

                    <td class=" "><input type="text" name="rate"  ng-model='rate'> </td>

                </tr>
                <tr>
                    <td>         
                        <select name="item1"  ng-model="item1" >
                            <option value="">Choose..</option>
                            <option value="T-SHIRT">T-SHIRT</option>
                            <option value="JACKET">JACKET</option>

                        </select>
                    </td>

                    <td class=" "><input type="text" name="rate1"  ng-model='rate1'> </td>

                </tr>


            </tbody>

        </table>
        <button type="button"  ng-click='saveorder()' >SAVE ORDER</button>
    </div>    

 <script>
        Myapp.controller('orderFormController', ['$scope', '$http', function ($scope, $http) {
            var data = {};
            data['item'] = [];
            $scope.saveorder = function () {
                var rowCount = $('#item_table tr').length;
                for (i = 1; i < rowCount; i++) {
                    data['item'][i] = {'item': $scope.item + i, 'rate': $scope.rate + i}
                    //data['item'][i] = {'item': $scope.item,  'rate': $scope.rate}
                }
            }

        });
    </script> 

在上面的for循环中,第二个推荐的行正在工作,它返回item的值,但我希望得到表中的所有项值。当我在循环中使用item + i时,它返回一个空值意味着它不像$ scope.item1,$ scope.item2等那样工作。 我是棱角分明的新手吗?提前谢谢

1 个答案:

答案 0 :(得分:1)

尝试更改您的列和类型名称以使用order_no [0],order_no 1等...并键入[0],键入1等等。并检查下面的代码:

var rowCount = $('#item_table tr').length;
for (i = 0; i < rowCount; i++) {
    order_no[i] =  $scope.order_no[i];
    type[i] =  $scope.type[i];
}

您可以根据代码检查小提琴https://jsfiddle.net/qh73ogzw/30以检查更改的示例