onoffswitch-checkbox

  @foreach (EmailSubscription es in Model)
   {

if(true){

<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="24px;border-radius:12px;"></span>
         </label>
 </div>

}

else

{

        <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="24px;border-radius:12px;"></span>
         </label>

}

}

js中对多个onoffswitch-switch 按钮 进行数组形式的赋值

<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");
        }

                       

原文地址:https://www.cnblogs.com/yi-miao/p/8058109.html