Javascript 如果未选中复选框,如何禁用按钮

Javascript 如果未选中复选框,如何禁用按钮,javascript,jquery,angularjs,model-view-controller,input,Javascript,Jquery,Angularjs,Model View Controller,Input,我得到了以下角度模板: <tr ng-repeat="user in users"> <td><input type="checkbox"></td> <td>{{user.username}}</td> <td>{{user.apiKey}}</td> <td>{{user.apiSecret}}</td> </tr> ... &l

我得到了以下角度模板:

<tr ng-repeat="user in users">
    <td><input type="checkbox"></td>
    <td>{{user.username}}</td>
    <td>{{user.apiKey}}</td>
    <td>{{user.apiSecret}}</td>
</tr>
...
<button type="button" class="btn btn-danger"><i class="fa fa-trash-o"></i></button>

{{user.username}
{{user.apiKey}
{{user.apiSecret}}
...
如果未选中复选框,如何禁用该按钮,但在选中表中的一个或多个复选框时启用该按钮


如何确定检查了哪个表条目并访问了用户数据?

角度方法是使用
ng disabled
。例如,您可以使用以下内容:

ng-disabled="!checked"
这个!意思是做相反的事,所以!真=假=真

完整示例:

<td>
   <input type="checkbox" ng-model="checked"
</td> 

<button class="btn btn-danger" ng-disabled="!checked"><i class="fa fa-trash-o"></i>Submit</button>


在一个非angularJS示例中(与@GothBurz的答案相比,后者更优雅,但不那么直观):

jQuery:

$("input:checkbox").change(function() {     // every time a checkbox changes
    $("button").attr("disabled", "true");   // start button disabled
    $("input:checkbox:checked").each(function() {
        $("button").removeAttr("disabled"); // if any checkbox checked, make button enabled
    });
});
请参见一个工作示例


或者,有关更长的非jQuery示例,请参见。

您可以将
选中状态添加到用户,并在每次复选框更改时选中(如果仍然选择了一个用户)

可以使用
angular.forEach
、for循环或下划线.js(如果您想使用它)进行检查。 在循环过程中,还可以创建选中用户的数组

请看下面或里面的演示

angular.module('demoApp',[])
.控制器(“主控制器”,主控制器);
功能主控制器($scope){
$scope.users=[{
用户名:“John”,
apiKey:'1234',
秘密:“2345”
}, {
用户名:“Jane”,
apiKey:'234',
秘诀:“24”
}];
$scope.checkedUsers=[];
$scope.checkButtonState=函数(){
/*使用下划线.js
$scope.checkedUsers=\.where($scope.users,{check:true});
$scope.enableButton=\uu0.chain($scope.checkedUsers)
.pluck('check').some().value();
*/
$scope.checkedUsers=[];
angular.forEach($scope.users,function(user){
如果(user.check){
$scope.checkedUsers.push(用户);
}
});
$scope.enableButton=$scope.checkedUsers.length>0;
};
}

{{user.username}
{{user.apiKey}
{{user.apiSecret}}
{{checkedUsers}}

什么是“检查”的,它从哪里来?这似乎对我不起作用:
但我检查它时什么也没发生。那么,还有其他问题,您是否调用了
ng app
?我看不到你剩下的代码可以告诉你。我更新了我的笔,以证明它工作正常。但问题中没有标记jQuery;除非angular.js自动需要jQuery?我没有使用angular,所以我真的不知道。@DavidThomas这可以很容易地转换为普通JS。我将提供另一个例子。如何使用整个jQuery函数而不是一行角度更优雅?@DavidThomas happier now?更快乐的xP,wy提供的漂亮jQuery解决方案!我偷了它,并把它作为要点保存了下来。