1

我工作的asp.net MVC 5,我已经在input场添加checkbox类型的bootstrap toggle switch像波纹管Asp.net MVC检查/取消选中基于值的复选框?

<input id="test_id" name="cmdName" type="checkbox" checked data-toggle="toggle"> 

我想要做的检查/取消选中基于复选框在

注:默认情况下,它总是在

我想要做这样的事情在我的JavaScript

If(command == "On") 
{ 
    //then it will check the checkbox 
} 
else if (command =="Off") 
{ 
    //then it will uncheck the checkbox  
} 

我发现与此相关的很多类似的问题,我想实现它,但一切都是徒劳

的链接是波纹管

  1. Link 1
  2. Link 2
  3. Link 3
  4. Link 4

我也有尝试添加$('input:checkbox[name=cmdName]').attr('checked', false);包括setAttributeremoveAttribute.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> 

任何帮助,将不胜感激

回答

0

退房THIS fiddle

我用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); 
}); 
+0

我使用jQuery的都和3.1.0铬,还使用了.prop但没有用 – faisal1208

0

您是否检查控制台是否有任何错误?我会说你的脚本的顺序是错误的。尝试在bootstrap.min.js之后包含bootstrap-toggle.js脚本。

如果没有错误,请尝试切换这样的复选框:

$('#test_id').bootstrapToggle('on') 

或者

$('#test_id').bootstrapToggle('off') 
+0

控制台没有错误 – faisal1208

+0

我添加了用于设置复选框状态的方法,试试看。 – Dygestor

+0

是的,已经尝试过,但没有工作 – faisal1208