当-6ren">
gpt4 book ai didi

javascript - 获取 Angular JS中的复选框数组值

转载 作者:行者123 更新时间:2023-12-03 07:44:45 25 4
gpt4 key购买 nike

我看到一些帖子问了同样的问题,但我无法让它发挥作用。我对 Angular 很陌生,所以我需要帮助。
我正在尝试插入带有标签的新收入。
我从服务中获取这些标签并像这样显示它们:

<label ng-repeat="tag in tags">
<input type="checkbox" ng-model="tags_chosen" name="tags_chosen[tag]" ng-true-value="<%tag.id%>"/> <%tag.name%>
</label>

当我尝试以 Angular 取回复选框值时,它不起作用:

 this.addIncome = function($scope) {
var data = {
'project_id':$scope.project_id,
'amount':$scope.amount,
'payment_date':$scope.payment_date,
'tags':$scope.tag_chosen,
'description':$scope.description,
'type':$scope.type
};

return $http.post(URL.BASE_API + 'income/store',data).
success(function(response) {
ServicesStatus.return = response;
}).error(function(response) {
console.log('Service error');
});
};

我怎样才能做到这一点?

谢谢!

最佳答案

试试这个:

$scope.tag_chosen =[];

$scope.toggleSelection = function ( deviceId, $event ) {
var checkbox = $event.target;
var action=(checkbox.checked ? 'add':'remove');

var idx = $scope.tag_chosen.indexOf( deviceId );

// is currently selected
if (action=='remove' && idx != -1 ) {

$scope.tag_chosen .splice( idx, 1 );

}

// is newly selected
if (action=='add' && idx == -1 ) {

$scope.tag_chosen.push( deviceId );
}

在 html 中 >>

ng-click="toggleSelection(yourcjeckbox value,$event)"

关于javascript - 获取 Angular JS中的复选框数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35257208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com