如何在未选中项目时禁用该按钮
How to disable the button when item are not checked?
我有多个与按钮单击事件相关联的复选框项。但我试图禁用按钮时,没有复选框项被选中。下面是我的代码:
<script>
$( document ).ready(function() {
$('#cbOne').click(function(){
if($(this).attr('checked') == false){
$('#target-group').attr("disabled","disabled");
}
else {
$('#target-group').click(function() {
var targetGroups = $("input:checkbox:checked", ".actors-$i").map(function() {
return $(this).val();
}).get();
$('#out').append(targetGroups.join(','));
});
}
});
});
</script>
<button class="aui-button" id="target-group" style="float: right">Generate Target Group</button>
代码工作正常,但按钮是启用的,即使没有复选框被选中
尝试:
$('#target-group').attr("disabled", true);
如果不成功尝试:
$('#target-group').prop("disabled", true);
试一下:
$(document).ready(function() {
$('#target-group').click(function() {
if ($('input:checkbox:checked').length == 0) {
return;
}
var targetGroups = $("input:checkbox:checked", ".actors-$i").map(function() {
return $(this).val();
}).get();
$('#out').append(targetGroups.join(','));
});
});
演示试试这个:
$(document).ready(function () {
//To check whether the checkbox is checked or not
$('input[type="checkbox"]').click(function ()
{
if ($(this).prop("checked") == true) {
$("#target-group").show();
}
else if ($(this).prop("checked") == false) {
$("#target-group").hide();
}
}
});
相关文章:
- 使用JavaScript或jQuery检查单选按钮选择是否与数组中的项目匹配
- 更新:仅根据单选按钮和所选选项选择特定项目
- 如何在猫头鹰旋转木马中滑动所有可见项目,而不是使用上一个/下一个按钮
- 单击按钮可更改多个跨度项目
- Codrops页面转换.如何创建单独的按钮,而不是列表项目的效果
- 使用按钮使项目可见
- jQuery Mobile列表视图,将投票按钮添加到行项目中
- Javascript检查循环中项目的所有按钮
- 通过单击按钮将选项卡(项目)添加到选项卡面板,扩展
- 在 SharePoint 2010 上创建“添加新项目”按钮
- 尝试换行按钮和项目
- 如果在选择列表中选择了 4 个以上的项目,如何禁用按钮
- 使用 UI 可排序,在单击按钮时根据给定值将项目移动到新位置
- 当用户点击后退按钮时,如何加载项目
- ExtJS 5 - 从商店动态将项目添加到拆分按钮菜单
- 文件上传按钮在科尔多瓦/Phonegap项目中仍然不起作用
- 单选按钮组显示所选值 jQuery 上的项目
- 按按钮从阵列中删除项目
- 用于将多个下拉列表中的选定项目复制到文本框的按钮
- 下一个和上一个按钮将在OWL旋转木马上滑动2个项目