我想你有你的參考this
困惑。如果我沒有理解你的問題,這應該做你想要什麼:
function levels() {
var $this = $(this); // Cache a reference to the element that was clicked
if ($this.is(':not(:checked)') { // Determine if the clicked element is checked
$(':input').each(function() { // Loop through every input element
// Here, 'this' is the current element in the loop and
// '$this' is the originally clicked element.
if (this.title === ('RQLevel' + $this.attr('src'))) {
$(this).removeAttr('checked');
}
});
}
}
更新:
我應該意識到這個較早,但你的主要問題是使用src
屬性爲基礎你的比較。 src
屬性被解析,因此當您查詢它時,該值將是絕對URL。也就是說,而不是值「2」,您將有值「http://example.com/2」。所以,你想使用一個data attribute。查看你的jsfiddle的我的update作爲一個工作示例。
此外,我沒有使用onclick
屬性來註冊事件處理程序,而是使用jQuery的.bind()方法來綁定處理程序。這裏是更新的JavaScript:
$(function() {
$(':input').bind('click', function() {
var src = $(this).data('src');
if (!this.checked) {
$('input:checked').each(function(){
if (this.title === ('RQlevel' + src)) {
this.checked = false;
}
});
}
});
});
這有點不清楚,你能提供一個例子嗎? – Brayn 2011-01-27 13:40:15
http://jsfiddle.net/V8FeW/ – 2011-01-27 14:26:02