Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery手风琴设置处于活动状态_Jquery_Jquery Ui - Fatal编程技术网

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

jQuery手风琴设置处于活动状态,jquery,jquery-ui,Jquery,Jquery Ui,我在ASP:NETMVC应用程序的页面中有一个jQuery手风琴,我想在运行时在其中设置活动的手风琴 我的代码如下: <script type="text/javascript"> $(document).ready(function() { var accordionindex = $("#UIPViewModel_ActiveAccordion").val(); alert("Setting active index to " + acco

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

我的代码如下:

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

$(文档).ready(函数(){
var accordionidex=$(“#UIPViewModel_ActiveAccordion”).val();
警报(“将活动索引设置为“+accordionindex”);
$(“手风琴”)。手风琴(“激活”,手风琴索引);
});
您将看到最后一行设置活动手风琴当我使用此代码时它始终表现为我使用了active:false和所有手风琴都关闭,即使警报显示正确的运行时值

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

$(“#accordion”).accordion('activate',$(“#UIPViewModel#u ActiveAccordion”).val()

当我将最后一行更改为:

$(“手风琴”)。手风琴(“激活”,2);(即硬编码)。它总是工作正常

有人能看出哪里出了问题吗?
我犯错误的地方???

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

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

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

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

$(函数(){
var accordionidex=数字($(“#UIPViewModel_ActiveAccordion”).val();
警报(“将活动索引设置为“+accordionindex”);
$(“手风琴”)。手风琴(“激活”,手风琴索引);
});

如果val()可能不是普通数字(比如,如果它的值有“索引4”),那么实际上只需要使用parseInt。您可以在设置变量时将其转换为数字。

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

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

我花了一段时间才发现我不是疯了——jQueryUI 1.8支持激活方法。他们在1.9或更高版本中删除了它。

实际上,该值来自一个用int值填充的隐藏字段。似乎隐藏字段值必须作为字符串返回。当值从DOM中出来时,它们总是字符串。即使您将它们转换为整数或浮点数,然后从js将它们注入DOM。我通过艰苦的方式了解到了这一点,这就是问题所在。另外,当我再次看到相同的页面时,使用activate会导致所选手风琴出现不太令人满意的幻灯片。我从这里的另一篇文章中得到了一个关于“销毁”的提示,然后在选择活动的手风琴的情况下重新创建手风琴,页面会根据需要刷新,没有任何手风琴UI效果。我使用:$(“#手风琴”)。手风琴(‘销毁’)$手风琴({active:parseInt(accordionidex)});几个小时以来,我一直在试图找到解决这个问题的办法!像字符串和整数一样简单的东西。哇!