angularjs以逗号分隔值获取表单值的方式

时间:2014-10-03 11:31:30

标签: javascript jquery angularjs

我有一个这样的表格

<form id="myForm">
    <input type="text" name="userName" value="test" />
    <ul>
      <li ng-repeat="item in list">
            <input type="checkbox" name="chkList" value="id" />
      </li>
    </ul>
</form>

我正在使用jquery提交表单并转换像这样的帖子数据

var d = 'name=' + $('#myform input[name=userName]).val();
var valArr = [];
$('#myform input[name=chkList]').each(function() {
    valArray.push($(this).val());
});
d = '&listOfIds=' + valArray.join(',');
... then submitting post with data: d

我不想使用jquery但不确定angualr等价物会在这里?

2 个答案:

答案 0 :(得分:1)

实际上它的全部细节,可能是这样:

 <input type="checkbox" name="chkList" ng-model="valArray[$index]" value="id" />

并在控制器中:

 $scope.valArray = [ 1 , 2 , 3 ];

答案 1 :(得分:0)

以下是您要找的内容:JSFiddle

HTML

<div ng-app="myApp" ng-strict-di>
<div ng-controller="Ctrl">
    <div ng-repeat="item in items">
        <input type="checkbox" ng-model="$parent.values[item.id]"/>{{item.name}}
    </div>
    <button type="button" ng-click="submit()">Submit</button>
</div>

JS:

angular.module("myApp",[]).controller("Ctrl",function($scope){

    angular.extend($scope,{
        values:{},
        items:[
            {id:1,name:'name1'},
            {id:2,name:'name2'},
            {id:3,name:'name3'},
            ],
        submit: function() {
            var d = '&listOfIds=' + Object.keys($scope.values).join(",");
            alert(d);

        }
    });
});