gpt4 book ai didi

jquery - 复选框的行为类似于切换开关

转载 作者:行者123 更新时间:2023-12-01 07:42:20 25 4
gpt4 key购买 nike

我有一个 ASP.NET 应用程序,它在页面中有一个产品列表,其中包含相关详细信息和状态列​​(位字段 - 复选框)。它工作得很好,我可以使用 Ajax 将状态从 true 更改为 false,反之亦然。这样做时,我保持复选框的状态意味着如果它为真,则它保持选中状态,否则保持未选中状态。请参见下图,除了红色突出显示的部分(还有一件事 - True 表示处于事件状态,False 表示在这种情况下处于非事件状态):

Sample Image

现在我正在尝试将复选框也设置为切换开关,以保持复选框的更改,如下所示 - BootStrap Toggle Switches

我不知道该怎么做,并尝试按照此操作使复选框用作使用类的切换开关,如下 - jQuery Toggle Switches

<script>
$(document).ready(function () {
$('.toggle-checkbox').btnSwitch({
Theme: 'Light',

OnText: "On",
OffText: "Off",

OnValue: true,
OffValue: false
});
});
</script>

注意:我必须保留图像中显示的复选框的状态,这些复选框不会像“true”、“checked”那样以红色突出显示,反之亦然。我很高兴知道是否可以使用拨动开关执行相同的操作。

数据库脚本:

CREATE TABLE [dbo].[Products](
[Id] [int] IDENTITY(1,1) PRIMARY KEY,
[ProductName] [nvarchar](MAX) NOT NULL,
[Time_Posted] [nvarchar](MAX) NOT NULL,
[Status] [bit] NOT NULL
)

INSERT INTO Products VALUES
('Denim', '7:30:10 PM', 1),
('Pringles', '8:00:00 PM', 1)

型号:

public class Product
{
public int Id { get; set; }
public string ProductName{ get; set; }
public string Time_Posted { get; set; }
public bool Status { get; set; }
}

Controller :

//Get details of the products
[HttpGet]
public ActionResult Index()
{
MainDbContext db = new MainDbContext();

var con = (from c in db.Products
select c).ToList();

return View(con);
}

//Update status of the products like active or inactive
[HttpPost]
public JsonResult UpdateStatus(int id, bool status)
{
MainDbContext db = new MainDbContext();
var result = db.Lists.Find(id);

if (result != null)
{
result.Status = status;
db.Entry(result).State = EntityState.Modified;
db.SaveChanges();
}
return Json(true);
}

查看:

@model SampleApp.Models.Product
@{
ViewBag.Title = "Index";
}

<link href="~/Scripts/jquery.btnswitch.css" rel="stylesheet" />
<script src="~/Scripts/jquery-3.1.1.slim.min.js"></script>
<script src="~/Scripts/jquery.btnswitch.js"></script>

<div id="divData">
<table class="table table-bordered table-condensed" id="data">
<thead>
<tr>
<th style="text-align:center;" class="hide">ID</th>
<th style="text-align:center;">Products</th>
<th style="text-align:center;">Time Posted</th>
<th style="text-align:center;">Status</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Items.Count; i++)
{
<tr>
<td id="ID" style="text-align: center;" class="hide">@Html.DisplayFor(modelItem => Model.Items[i].Id)</td>
<td style="text-align: center;">@Html.DisplayFor(modelItem => Model.Items[i].ProductName)</td>
<td style="text-align: center;">@Html.DisplayFor(modelItem => Model.Items[i].Time_Posted)</td>
<td style="text-align: center;">@Html.CheckBoxFor(modelItem => Model.Items[i].Status, new { @class = "toggle-checkbox", data_id = Model.Items[i].Id })</td>
</tr>
}
</tbody>
</table>
</div>

<script type="text/javascript">
var url = '@Url.Action("UpdateStatus")';
$('.toggle-checkbox').click(function () {

var isChecked = $(this).is(':checked'); //CheckBox checked - True or false
var id = $(this).data('id'); //Get the id of that specific checked row

$.post(url, { id: id, status: isChecked }, function (response) {
if (response) {
alert("Status changed");
}
})
});
</script>

<script>
$(document).ready(function () {
$('.toggle-checkbox').btnSwitch({ //This is the script for toggling
Theme: 'Light',

OnText: "On",
OffText: "Off",

OnValue: true,
OffValue: false
});
});
</script>

最佳答案

您可以在 jQuery Toggle Switches 中使用事件

$('.toggle-checkbox').btnSwitch({
OnValue: 'On',
OnCallback: function(val) {
//your ajax code here
},
OffValue: 'Off',
OffCallback: function (val) {
//your ajax code here
}
});

使用此代码更改切换:

$('#toggle-checkbox').btnSwitch({
ToggleState:true //for switch on / true
});

$('#toggle-checkbox').btnSwitch({
ToggleState:false //for switch off / false
});

关于jquery - 复选框的行为类似于切换开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45530527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com