jQuery 手风琴设置处于活动状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1408261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:22:49  来源:igfitidea点击:

jQuery Accordion setting active

jqueryjquery-ui

提问by Redeemed1

I have a jQuery accordion in a page in ASP:Net MVC application in which I want to set the active accordion at runtime.

我在 ASP:Net MVC 应用程序的一个页面中有一个 jQuery 手风琴,我想在运行时设置活动的手风琴。

My code is as follows:

我的代码如下:

<script type="text/javascript">
    $(document).ready(function() {
        var accordionindex = $("#UIPViewModel_ActiveAccordion").val();
        alert("Setting active index to " + accordionindex);
        $("#accordion").accordion('activate', accordionindex );
    });
</script>

You will see the last line sets the active accordion. When I use this codeit always acts like I have used active : false and ALL of the accordions are closedeven though the alert displays the correct runtime value.

您将看到最后一行设置了活动的手风琴。当我使用这段代码时,它总是表现得好像我使用了 active : false 并且所有的手风琴都关闭了,即使警报显示了正确的运行时值。

I have also tried simply using the following which is the same:

我也尝试过简单地使用以下相同的内容:

$("#accordion").accordion('activate', $("#UIPViewModel_ActiveAccordion").val());

$("#accordion").accordion('activate', $("#UIPViewModel_ActiveAccordion").val());

When I change the last line to :

当我将最后一行更改为:

$("#accordion").accordion('activate', 2 ); (i.e. hard-coded). It always works correctly!

$("#accordion").accordion('activate', 2); (即硬编码)。它总是正常工作!

Can anyone see what′s wrong? Where i am making my mistake??

任何人都可以看到有什么问题吗?我在哪里犯了我的错误?

回答by Ken Browning

The variable being returned by val()is a string and accordion is expecting a number. Try:

返回的变量val()是一个字符串,手风琴需要一个数字。尝试:

    var accordionindex = $("#UIPViewModel_ActiveAccordion").val();
    accordionindex = parseInt(accordionindex, 10);  // convert a string to a number
    alert("Setting active index to " + accordionindex);
    $("#accordion").accordion('activate', accordionindex );

回答by Anthony

Just to extend Ken's solution:

只是为了扩展肯的解决方案:

<script type="text/javascript"> 
$(function () {
    var accordionindex = Number($("#UIPViewModel_ActiveAccordion").val());
    alert("Setting active index to " + accordionindex);
    $("#accordion").accordion('activate', accordionindex );
});
</script>

You really only need to use the parseInt if there is some chance that the val() will ever be something other than normal digits (like if it had "index 4" for the value). And you can cast it as a digit while you set the var.

你真的只需要使用 parseInt 如果 val() 有可能不是普通数字(比如它的值有“索引4”)。您可以在设置 var 时将其转换为数字。

回答by Gerard ONeill

for newer versions, the way to activate (and deactivate) the panel has changed:

对于较新版本,激活(和停用)面板的方式已更改:

jQuery(".selector").accordion("option", {active: index});

Took me a while to figure out I wasn't crazy -- jQueryUI 1.8 supports the activate method. They got rid of it for 1.9 and above.

我花了一段时间才发现我没有疯——jQueryUI 1.8 支持 activate 方法。他们在 1.9 及更高版本中摆脱了它。