无法测试每个循环中检查的输入

时间:2015-04-27 17:20:09

标签: javascript jquery html checkbox

我有一个输入复选框元素的简单对象,并希望检查每个循环中哪些被检查和做什么。复选框位于表格中多个tr的{​​{1}}个。{/ p>

这是我的代码:

td

如果我在每个循环中使用console.log $('table.occupancy-table').on('change', '.minimum_stay input', function() { var minimumStayInputs = $(this).closest('tr').find('input'); $(minimumStayInputs).each(function(){ if(this.prop('checked')) { //do stuff } } } ,我会得到如下数据:

this ...

但我总是从<input id="R11520" type="checkbox" name="minimum_stay[152][1442786400]" value="checked"> <input id="R11521" type="checkbox" name="minimum_stay[152][1442872800]" value="checked">收到错误Uncaught TypeError: this.prop is not a function。我也尝试了this.prop('checked')同样的结果。

任何想法可能是什么原因?

如果你需要样本html或者我应该创建小提琴,请告诉我。

1 个答案:

答案 0 :(得分:5)

循环中的

(function(angular) { 'use strict'; var myAppModule = angular.module('myApp', []); myAppModule.controller('myController', function($scope) { $scope.directives = [{ id: 'my-directive1', label: 'My Directive1', data: 'Directive 1 data.' }, { id: 'my-directive2', label: 'My Directive 2', data: 'Directive 2 data.' }]; $scope.selectedDirectiveId = $scope.directives[0].id; $scope.selectedDirectiveData = $scope.directives[0].data; $scope.selectChanged = function() { for (var i = 0, len = $scope.directives.length; i < len; i++) { if ($scope.directives[i].id == $scope.selectedDirectiveId) { $scope.selectedDirectiveId = $scope.directives[i].id; $scope.selectedDirectiveData = $scope.directives[i].data; break; } } }; }); myAppModule.directive('loaddirective', function($compile) { return { restrict: 'A', scope: { loaddirective: "=", directivedata: "=" }, link: function($scope, $element, $attr) { var value = $scope.loaddirective; if (value) { $element.html("<div " + value + " directivedata='directivedata'></div>"); $compile($element.contents())($scope); } }, controller: ['$scope', function($scope) { } ] }; }); myAppModule.directive('myDirective1', function() { return { templateUrl: 'my-directive1.html', scope: { directivedata: "=" } }; }); myAppModule.directive('myDirective2', function() { return { templateUrl: 'my-directive2.html', scope: { directivedata: "=" } }; }); })(window.angular); 是本机DOM元素,从jQuery中解包。

必须再次包装

this

您也可以使用原生$('table.occupancy-table').on('change', '.minimum_stay input', function() { var minimumStayInputs = $(this).closest('tr').find('input'); $(minimumStayInputs).each(function(){ if ( $(this).prop('checked') ) { } }); }); ,或者如果您正在尝试计算它们

this.checked