$(this).parent()。siblings(' .classname')。show()不起作用

时间:2014-07-22 21:36:07

标签: jquery

HTML:

<div class="quiz-template">
    <span class="question">1. What is the first word in the first chapter of the series?</span>

    <div class="image-container">
        <img src="img2/q1.png" />
    </div>

    <div class="answer-container">
        <div class="a-choice1">
            <input type="radio" name="qa1" value="0" />
            <label for="btn1"></label>
            <span>The</span>

        </div>

        <div class="a-choice1">
            <input type="radio" name="qa1" value="0" />
            <label for="btn2"></label>
            <span>Albus</span>
        </div>

        <div class="a-choice1">
            <input type="radio" name="qa1" value="0" />
            <label for="btn3"></label>
            <span>Number</span>
        </div>

        <div class="a-choice1">
            <input type="radio" name="qa1" value="1" />
            <label for="btn4"></label>
            <span>Mr.</span>
        </div>
    </div>

    <div class="result-container" id="r1">
        <!--
        <span class="tic mark">✓</span> 
        <span class="cross mark">X</span>
        -->
        <span class="result-text"></span>
        <span class="result-description">
            The first sentence reads, “Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they were perfectly normal, thank you very much.”
        </span>
    </div>
</div>

我正在尝试显示由display:none隐藏的结果容器类;通过功能

$('.a-choice1').click(function () {
    $(this).parent().siblings('.results-container').show("fast"); 
});

但它不起作用。我的遍历代码出了什么问题?另外,有没有一种方法可以让选择器中的值动态化,这样我就可以循环了?有点像:

$(this).parent().siblings('div[id=r '+N+']').show("fast");

其中N是一个整数,可以递增值来访问ids r1,r2,r3 ......等等?

2 个答案:

答案 0 :(得分:0)

尝试查找方法

$(this).parent().find('div#r'+N).show("fast");

答案 1 :(得分:0)

试试这个:

$('.a-choice1').find("input[type=radio]").click(function () {
    $(this).parents(".answer-container").next(".result-container").show("fast");
});

parents()(带选择器):http://api.jquery.com/parents/

next()(带选择器):http://api.jquery.com/next/

这是ya的jsfiddle:

http://jsfiddle.net/rePgM/

要做一个循环,它看起来像这样:

$(this).parent().siblings('.result-container').each(function(e, i){
     $(this).show("fast");  //$(this) now refers to each instance of the parent's siblings
});

这也消除了您发送ID号的需要。