使用按钮显示某些元素

时间:2011-04-06 09:50:31

标签: jquery parameters show

此处的代码:http://jsfiddle.net/B7Y43/

Hello fello程序员,

我有以下情况:

我的PHP脚本生成多个DIV,包含:

  • 一个问题(类为mainQuestion或subQuestionOf [questionID])
  • 按钮
  • 包含答案的段落

我正在努力实现以下目标:

当用户点击问题旁边的按钮时,答案和子问题应该淡入。我已经设法在用户点击按钮时使用$(this).next('p').fadeIn("fast");淡出答案  但由于subQuestions很少是下一个div,我想我需要使用参数。

所以在伪代码中:

<question id="1" subquestionOf=""><button>
<answer>
<question id="2" subquestionOf="1"><button>
<answer>
<question id="3" subquestionOf="1"><button>
<answer>

if user clicks button {
show answer;
show questions where subquestionof=parent.id;
}

我不知道如何使用参数,我试着获取按钮的父div的ID而没有运气:

$(function getID(event){
            var id = $(this).parent().attr("id");
            alert(id);
    });

提醒显示:“未定义”

2 个答案:

答案 0 :(得分:0)

尝试使用event.target中的event object获取所点击按钮的ID。

您是否正在使用Firebug进行调试,以检查$(this)的值和.parent()的结果?

答案 1 :(得分:0)

我试过(在jsFiddle中修改你的代码):

$("input.showAnswerButton").click(function() {
        $(this).next('p').fadeIn("fast"); //answer
        //subquestions
        $('.subQuestionOf'+$(this).parent().attr("id")).fadeIn("fast");
    });

它正在工作......