簡體   English   中英

使用其他方式將撥動開關從打開更改為關閉或從關閉更改為打開

[英]Changing toggle switch from On to Off or Off to On using if else

我正在asp.net MVC 5上工作。我已放置一個具有input type checkbox的引導程序切換按鈕。

波紋管是我的撥動開關的圖像

在此處輸入圖片說明

從我的查詢中,我得到一個命令名稱cmd_Name ,其中有OnOff值,並且基於此值,我只想將撥動開關從On-Off或Off-On更改

波紋管是我的剃刀句法切換

@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>}

波紋管是我的切換腳本

<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>

更新的代碼

貝婁是我更新的代碼和錯誤的圖像

在此處輸入圖片說明

更新了代碼2

我在布局的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>

參見bundle.config

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

我已經嘗試了很多東西, if...else$("#test_id").checked = true/false我也嘗試了document.getElementById("test_id").checked

但一切都是徒勞的,將不勝感激

嘗試這個 -

if(cmd_Name == "On") {
    $("#test_id").attr("checked", "checked");
} else if(cmd_Name == "Off") {
   $("#test_id").removeAttr("checked");
}

你也可以這樣

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM