我正试图在单击单选按钮时激活特定图像上的toggleClass()。我一直试图让它工作,但不能。我可以设法选择单选按钮的父元素,但没有任何操作。我尝试过next(),nextAll()和nearest()。所有这些似乎都没有达到我想要达到的目的。为什么最近的()选择器在这种情况下不起作用?
这里是我的jQuery函数:
$(document).ready(function() {
$("input[type=radio]").change(function() {
if (this.value == "AM") {
$(this).closest('img[title="am"]').toggleClass('houdini');
} else if (this.value == "PM") {
alert('PM');
/* Something here */
} else {
return false();
}
});
});
和我的HTML:
<tr>
<td><input type="radio" name="bisley" value="AM" />AM<input type="radio" name="bisley" value="PM" />PM<span class="compname">Bisley</span></td>
<td>Fit more into less space</td>
<td class="time"><img src="/images/generic/openday/tick.png" alt="tick" title="am" class="houdini" />10.00AM</td>
<td class="time"><img src="/images/generic/openday/tick.png" alt="tick" title="pm" class="houdini" />1.30PM</td>
</tr>
因为'input'和'img'正在被它们包含的'td'和你所转换的函数分开按照您的预期工作。 – BoltClock 2011-06-05 16:11:17