我想你有你的参考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