在回发后保持可折叠Div打开

时间:2016-04-09 15:19:44

标签: javascript html css postback collapse

我有一个包含一堆这些过滤器的面板。默认情况下,它们都已折叠,但是当您单击按钮时,它会展开div。但是,每次有回发时,div都会崩溃。如何让div的当前状态在回发中持续存在?

                <h4 class="contentFilterHeader">Start Date</h4>
                <button type="button" class="btn" data-toggle="collapse" data-target="#startDate">
                <div id="startDate" class="collapse">
                    <label for="from">Before:</label>
                    <div class="form-group">
                        <div class="date">
                            <div class="input-group">
                                <input type="text" class="form-control" id="startPicker" runat="server" name="date" />
                                <span class="input-group-addon><span class="glyphicon glyphicon-calendar"></span></span>
                            </div>
                        </div>
                    </div>

2 个答案:

答案 0 :(得分:1)

使用HiddenField存储面板的状态。

答案 1 :(得分:0)

您可以将runat=server添加到整个#startDate div,并在检查isPostBack之后从代码中将类设置为它们,或者更好地附加一些javascript代码以便在页面就绪时打开它< / p>

startDate.innerHtml = startDate.innerHtml + "<script> $(document).ready(function(){ $('#startDate').collapse('show') }) </script>";

假设您使用的是c#和bootstrap

修改

您可以使用以下代码 http://codepen.io/jammer99/pen/eZyqEN

var t,
  //read cookie and determine if any panel was open before
  earlierPanel = readCookie("openpanel")
  //if value is found collapse all earlier panels and then open the last panel
if (earlierPanel) {
  $('#accordion .panel-collapse').removeClass("in");
  $("#" + earlierPanel).addClass("in")
}
//reset the behaviour when user opens another panel
$('#accordion').on('shown.bs.collapse', function(w) {
  t = w.target;
  createCookie("openpanel", $(t).attr("id"))
})

// remove cookie value using the following or you can write your code to delete the cookie
$('#accordion').on('hide.bs.collapse', function(w) {  
  createCookie("openpanel","")
})

function createCookie(name, value) {
  document.cookie = name + "=" + value + "; path=/";
}

function readCookie(name) {
  var e = name + "=";
  var a = document.cookie.split(";");
  for (var d = 0; d < a.length; d++) {
    var f = a[d];
    while (f.charAt(0) == " ") {
      f = f.substring(1, f.length)
    }
    if (f.indexOf(e) == 0) {
      return f.substring(e.length, f.length)
    }
  }
  return null
};