在asp.net MVC中回发后保持JQuery Accordion当前窗格打开

时间:2017-10-17 04:10:07

标签: jquery asp.net-mvc

我在asp.net mvc应用程序中有jquery手风琴,我试图在回发后保持当前窗格打开。我尝试了这个Keep the current jQuery accordion pane open after ASP.NET postback?下提供的解决方案。但是,对我来说似乎没有用。

<input type="hidden" id="hidAccordionIndex" value="0" />
<script>
    $(function () {
      var activeIndex = document.getElementById("hidAccordionIndex").value;

      $("#accordion").accordion({
          heightStyle: "content",
          collapsible: true,
          navigation: true,
          active: activeIndex,
          change: function (event, ui) {
              var index = $(this).accordion("option", "active");
              $(document.getElementById("hidAccordionIndex")).val(index);
          }

      });
  } );
</script>

2 个答案:

答案 0 :(得分:0)

你的语法不正确:

$(document.getElementById("hidAccordionIndex")).val(index);

document.getElementById("hidAccordionIndex")返回相应HTML元素的原始id值(并且没有id选择器来定义目标元素的id属性),因此您需要使用jQuery id selector设置其值:

$("#hidAccordionIndex").val(index);

或者使用vanilla JS版本:

document.getElementById("hidAccordionIndex").value = index;

答案 1 :(得分:0)

<input type="hidden" id="hidAccordionIndex" value="0" />

<script>
    $(function () {
        var activeIndex = parseInt($("#hidAccordionIndex").val());

      console.log(activeIndex);

      $("#accordion").accordion({
          heightStyle: "content",
          collapsible: true,
          navigation: true,
          active: activeIndex,
          activate: function (event, ui) {
              var index = $(this).accordion("option", "active");
              console.log(index);
              $("#hidAccordionIndex").val(index);
          }

      });
  } );
</script>