这是一个有效的例子 - http://jsfiddle.net/3fPaC/。
代码 -
$(document).ready(function() {
var count = 0;
$("li").click(function() {
if($(this).siblings().hasClass("selected")) {
$(this).siblings().removeClass("selected");
count--;
$("#result").text("");
}
$(this).addClass("selected");
var question = $(this).attr("question");
$("#"+question).addClass("answered");
count++;
if(count == 3) {
$(".selected").each(function() {
$("#result").append(" "+$(this).text());
});
}
});
});
另外,不要只使用数字作为 ID,因为它可能会导致问题。
HTML 代码 -
<div id="result"></div>
<div id="question1002" class="question2" style="display: block; ">
<h1>Question 2</h1>
<p><i>This is Question 2</i></p>
<div class="answer-grid">
<ul class="answer">
<li question="question1002" answer="a1">Answer 1111</li>
<li question="question1002" answer="a2">Answer 2111</li>
<li question="question1002" answer="a3">Answer 3111</li>
</ul>
</div>
</div>
<div id="question1003" class="question2" style="display: block; ">
<h1>Question 2</h1>
<p><i>This is Question 2</i></p>
<div class="answer-grid">
<ul class="answer">
<li question="question1003" answer="a1">Answer 1</li>
<li question="question1003" answer="a2">Answer 2</li>
<li question="question1003" answer="a3">Answer 3</li>
</ul>
</div>
</div><div id="question1004" class="question2" style="display: block; ">
<h1>Question 2</h1>
<p><i>This is Question 2</i></p>
<div class="answer-grid">
<ul class="answer">
<li question="question1004" answer="a1">Answer 122</li>
<li question="question1004" answer="a2">Answer 222</li>
<li question="question1004" answer="a3">Answer 322</li>
</ul>
</div>
</div>
您可以将 count == 3 更改为您喜欢的任何值。3是问题的数量。