Asp.net MVC根据值选中/取消选中复选框
Asp.net MVC check/uncheck the checkbox based on a value?
我正在asp.net MVC 5上工作,我在input
字段中添加了checkbox
类型的bootstrap toggle switch
,如下面
<input id="test_id" name="cmdName" type="checkbox" checked data-toggle="toggle">
我想根据 on 或Off值
选中/取消选中复选框注意:默认总是在
我想在Javascript
中这样做If(command == "On")
{
//then it will check the checkbox
}
else if (command =="Off")
{
//then it will uncheck the checkbox
}
我发现了许多类似的问题,我试图实现它,但都是徒劳的
链接在下面
- 链接1
- 链接2 链接
- 3
- 链接4
我也试图添加$('input:checkbox[name=cmdName]').attr('checked', false);
包括setAttribute
, removeAttribute
, .prop
和许多其他,但无法完成任务,我不知道是什么问题
我使用jquery 3.1.0
,下面是我的参考
<head>
<title>@ViewBag.Title</title>
@*All the javascript and css files that are required by the
application can be referenced here so that there is no
need to refrence them in each and every view*@
<script type="text/javascript" src="~/Scripts/jquery-3.1.0.min.js"></script>
<script src="~/Scripts/bootstrap-toggle.js"></script>
<link href="~/Content/bootstrap-toggle.css" rel="stylesheet" />
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/DataTables/css/jquery.dataTables.min.css" rel="stylesheet" />
<script type="text/javascript" src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="~/Scripts/bootstrap.min.js"></script>
<script src="http://maps.google.com/maps/api/js?key=MyKey"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="icon" href="favicon.ico" type="image/ico" />
<link rel="shortcut icon" href="~/favicon.ico" /></head>
如有任何帮助,不胜感激
看看这个小提琴
我使用jquery 3.1.0和Chrome进行测试。
$("#toggler").on("click", function() {
var state = $('#myCheckbox').prop("checked");
if (!state)
$('#myCheckbox').prop("checked", true);
else
$('#myCheckbox').prop("checked", false);
});
您检查控制台是否有任何错误?我觉得你的剧本顺序不对。试着在bootstrap.min.js
之后包含bootstrap-toggle.js
脚本。
如果没有错误,尝试像这样切换复选框:
$('#test_id').bootstrapToggle('on')
或
$('#test_id').bootstrapToggle('off')
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 使用Jquery选择或取消选择ListView中的所有复选框
- 使用jquery选中/取消选中单个复选框
- 使用javascript在页面加载时取消选中所有复选框
- (取消)使用单独的复选框激活复选框列表
- jQuery-取消选中复选框
- 单击或取消选中复选框时,如何创建弹出的自定义窗口
- Javascript选中/取消选中所有复选框并将值写入文本区域
- dojo/cbtree取消选中所有复选框并选中选中复选框
- 通过选中和取消选中复选框来添加和删除数组中的值
- 当与之关联的 ng-model 为 false 时,Angularjs 复选框值不会取消选中
- 取消复选框未进行可视化更新
- 复选/取消复选框不能与prop功能一起工作
- 在剑道ui网格中上下滚动时取消复选框
- 检查条件后使用jQuery取消复选框
- 当另一个复选框被选中时,取消复选框,但如果被选中,也取消复选框
- JQuery .prop函数不工作,取消复选框
- 取消复选框绑定
- 如何在不取消复选框的情况下通过Ajax更新复选框列表
- 绑定点击事件后无法取消复选框