@foreach (EmailSubscription es in Model)
{javascript
if(true){java
<div class="onoffswitch">
<input type="checkbox" checked class="onoffswitch-checkbox" name="onoffswitch_1" id="onoffswitch_1">
<label class="onoffswitch-label" style="border-radius:23px;" name="onoffswitch_1" for="onoffswitch_1">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch" style="width:24px;border-radius:12px;"></span>
</label>
</div> ajax
}數據庫
elsec#
{數組
<input type="checkbox" class="onoffswitch-checkbox" name="onoffswitch_1" id="onoffswitch_1">
<label class="onoffswitch-label" style="border-radius:23px;" name="onoffswitch_1" for="onoffswitch_1">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch" style="width:24px;border-radius:12px;"></span>
</label>ide
}ui
}this
js中對多個onoffswitch-switch 按鈕 進行數組形式的賦值url
<script type="text/javascript">
$(document).ready(function(){
var active= new Array();
var num= new Array();
var i = 0;
$("label[name^='onoffswitch_']").on('click',function (e) {
var id = $(this).attr("name").split("_")[1];
var onswitch= document.getElementById("onoffswitch_" + id).checked;
if (onswitch)
{
active[i] = "false";
}
else
{
active[i] = "true";
}
num[i] = id;
i++;
});
$("#btnSave").click(function () {
if (active.length>0 && num.length>0)
{
$(".ajax-loader").show();
$.ajax({
url: ' ',
traditional: true, //阻止深度序列化,提交時後臺處理才能接收到數組值。默認爲 false,提交數組必須爲true
data:
{
active: active,
num: num
},
complete: function () {
$(".ajax-loader").hide();
}
});
}
});
});
</script>
c# 裏:control裏數組接受和處理,接收數組, 避免未修改時更新數據庫
public ActionResult SaveSubscriptions(string[] active, string[] num)
{
bool Isonline = false;
List<Num> list = _numService.GetNumByCustomerID(logginUser.UserID).ToList();
if (list.Count>0)
{
for (int i = 0; i < IsOnline.Length; i++)
{
var num = new Guid(num[i]);
num result = list.Where(a => a.ID == num).FirstOrDefault();
if(result.active != Boolean.Parse(active[i]))
{
if (active[i] == "true")
{
active = true;
}
else
{
active = false;
}
list.Add(result); active= true; } } } if (active) { _numService.UpdateNum(list); } return RedirectToAction("Index"); }