[英]Changing toggle switch from On to Off or Off to On using if else
I am working on asp.net MVC 5. I have placed a bootstrap toggle switch button having input type checkbox
我正在asp.net MVC 5上工作。我已放置一个具有input type checkbox
的引导程序切换按钮。
Bellow is the image of my toggle switch 波纹管是我的拨动开关的图像
From my query i am getting a command name cmd_Name
in which there is On or Off value in it and based of this value i just want to change the toggle switch from On-Off or from Off-On 从我的查询中,我得到一个命令名称cmd_Name
,其中有On或Off值,并且基于此值,我只想将拨动开关从On-Off或Off-On更改
Bellow is my razor syntax for toggle 波纹管是我的剃刀句法切换
@using (Html.BeginForm())
{
//var cmd_Name = Session["cmd_Name"];
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset style="height:60px">
<legend style="text-align:center; font-size:large; font-family:'Times New Roman'; background-color:#C8E6C9; color:red">Remote On/Off</legend>
<input id="test_id" name="cmdName" type="checkbox" checked data-toggle="toggle">
</fieldset>}
Bellow is my script for toggle 波纹管是我的切换脚本
<script type="text/javascript">
var search = '@Session["search"]';
var cmd_Name = '@Session["cmd_Name"]';
var data = {}
//alert(cmd_Name);
$(window).on('load', function () {
// here cmd_Name i having On or Off value
// i want to put a check like bellow
if(cmd_Name == "On")
{
// then the toggle switch (checkbox) moves to On
// what should place here that moves the switch from on to off and vise versa
}
else
{
//then toggle switch (checkbox) moves to Off
}
})</script>
Updated Code 更新的代码
Bellow is image for my updated code and error 贝娄是我更新的代码和错误的图像
Updated Code 2 更新了代码2
I have following declarations in my head
section in layout 我在布局的head
有以下声明
<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>
See bundle.config
参见bundle.config
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
I have tried many things in if...else
like $("#test_id").checked = true/false
also i have tried document.getElementById("test_id").checked
我已经尝试了很多东西, if...else
像$("#test_id").checked = true/false
我也尝试了document.getElementById("test_id").checked
But all in vain, any help would be appreciated 但一切都是徒劳的,将不胜感激
Try this - 尝试这个 -
if(cmd_Name == "On") {
$("#test_id").attr("checked", "checked");
} else if(cmd_Name == "Off") {
$("#test_id").removeAttr("checked");
}
You can do it this way as well 你也可以这样
if(cmd_Name == "On") {
$("#test_id").bootstrapToggle("on")
} else if(cmd_Name == "Off") {
$("#test_id").bootstrapToggle("off")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.