在ng-change上从选定的复选框中获取ID

时间:2017-09-11 13:13:29

标签: javascript jquery angularjs arrays

我正在尝试在选中它时提取复选框的ID,但我似乎无法找到适合我正在尝试的方法。

首先,我有复选框的HTML / Angular。复选框由三层生成。首先是服务级别,然后是星期几,然后是服务本身(这是复选框)。服务水平使手风琴,一周的日期被加载到选项卡,复选框本身正常进入。

<div class="delivery-rules">
    <div class="panel-group" id="accordion">
        <div class="panel panel-default" ng-repeat="level in settings.serviceLevels">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#{{level.LevelTmsCode}}">{{level.LevelName}}</a>
                </h4>
            </div>
            <div id="{{level.LevelTmsCode}}" class="panel-collapse collapse in">
                <div class="panel-body">
                    <ul class="nav nav-tabs">
                        <li id="{{day.Day}}-{{level.LevelTmsCode}}-tab" ng-repeat="day in settings.serviceDays">
                            <a id="{{day.Day}}-{{level.LevelTmsCode}}" href="#tabContent-{{day.Day}}-{{level.LevelTmsCode}}" ng-click="settings.changeTab(day, level, $event)">{{day.Day}}</a>
                        </li>
                    </ul>
                    <div class="tabContent" id="tabContent-{{day.Day}}-{{level.LevelTmsCode}}" ng-repeat="day in settings.serviceDays">
                        <h4>{{day.Day}}</h4>
                        <div class="time-check" ng-repeat="service in settings.services">
                            <input type="checkbox" value="None" ng-change="settings.showChecked(settings.rules, $event)" ng-model="settings.selected[$index]" class="time-check-input" id="{{level.LevelTmsCode}}-{{day.Day}}-{{service.TimeValidation}}" name="check"/>
                            <label for="{{level.LevelTmsCode}}-{{day.Day}}-{{service.TimeValidation}}" class="time-check-input"></label> <span>{{service.TimeValidation}}</span>
                        </div>
                    </div> 
                </div>
            </div>
        </div>
    </div>
</div>

构建这些复选框,制表符和手风琴的数组加载了来自标准http post请求的数据。然后,一旦完成,我将所有三个数组的所有可能组合放入一个大数组,并将其checked属性设置为false。

// Get Service Levels to Build Delivery Rules Accordion
settings.getDeliveryServices = function() {
    $http.get(resourceBase + "api/service/levels").success(function(data) {
        settings.serviceLevels = data;
        // Get Service Days
        $http.get(resourceBase + "api/service/days").success(function(days) {
            settings.serviceDays = days;
            // Build the Accordion
            setTimeout(() => settings.triggerClick(settings.serviceLevels), 500);
            $http.get(resourceBase + "api/service/services").success(function (services) {
                settings.services = services;
                // Build a collection of all possible rules
                for (var a = 0; a < settings.serviceLevels.length; a++) {

settings.rulesTmsCode.push(settings.serviceLevels[a].LevelTmsCode + "-");
                }
                for (var b = 0; b < settings.serviceDays.length; b++) {
                    settings.rulesDay.push(settings.serviceDays[b].Day + "-");
                }
                for (var c = 0; c < settings.services.length; c++) {

settings.rulesTime.push(settings.services[c].TimeValidation);
                }
                var allArrays = [settings.rulesTmsCode, settings.rulesDay, settings.rulesTime];
                function allPossibleCases(arr) {
                    if (arr.length === 1) {
                        return arr[0];
                    } else {
                        var result = [];
                        var allCasesOfRest = allPossibleCases(arr.slice(1)); 
                        for (var i = 0; i < allCasesOfRest.length; i++) {
                            for (var j = 0; j < arr[0].length; j++) {
                                result.push(arr[0][j] + allCasesOfRest[i]);
                            }
                        }
                        return result;
                    }
                }
                var uncheckedRules = allPossibleCases(allArrays);
                for (var i = 0; i < uncheckedRules.length; i++) {
                    settings.rules.push({
                        id: uncheckedRules[i],
                        checked: false
                    });
                }
            });
        });
    });
}

当选中每个框时,我正在尝试操作组合数组,以便将所选组合设置为true。

// Check and Filter Rules to send
settings.showChecked = function (object, $event) {
    for (var i = 0; i < settings.rules.length; i++) {
        if (settings.rules.hasOwnProperty(i)) {
            if (typeof settings.rules[i].id == settings.selected[i]) {
                settings.showChecked(settings.rules[i], settings.selected[i]);
            }
            if (settings.rules[i].id === settings.selected[i]) {
                settings.rules[i].checked = true;
            }
        }
    }
    console.clear();
    console.log(settings.rules);
}

目前,没有任何设置为true,因为我似乎无法从复选框中获取ID以将其与存储在组合数组的ID值中的字符串进行比较。所以基本上我需要选中复选框的ID,我需要将其传递给ng-change事件。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情......

<input id={{emp.name}} type=checkbox value="{{emp.name}}" ng-change="settings.showChecked(settings.rules, $event)>

 settings.showChecked=function(object,$event)
 {
   var el = event.target.id
 }

另一种方式是你可以在下面的地方事件中传递id值

<select id="hairColorComponent" ng-model="hairColor"
    ng-options="option.name for option in hairColorData" 
    ng-change="updateUserData('hairColorComponent')">


$scope.updateUserData = function (id) {
var element = jQuery('#'+id);
};