使用title和src属性的JQuery

使用title和src属性的JQuery,jquery,function,attributes,each,Jquery,Function,Attributes,Each,我在寻找一个脚本,该脚本将点击一个复选框,获得输入的src属性,然后遍历所有其他复选框和单选按钮。每个,并删除标题属性为'RQlevel'+this.src的任何输入的选中属性。希望这已经足够清楚了 这是我的尝试,但它是不正确的 function levels() { if ($(this).is(':not(:checked)').each(function()) { if($(':input').attr('title', 'RQlevel' + this.src) {

我在寻找一个脚本,该脚本将点击一个复选框,获得输入的src属性,然后遍历所有其他复选框和单选按钮
。每个
,并删除标题属性为
'RQlevel'+this.src
的任何输入的选中属性。希望这已经足够清楚了

这是我的尝试,但它是不正确的

function levels() {
   if ($(this).is(':not(:checked)').each(function()) {
        if($(':input').attr('title', 'RQlevel' + this.src) {
        $(':input').removeAttr('checked');
        });
   });    
} 

的工作示例尝试以下操作:

function levels() {
    if (!this.checked) {
        var src = this.src;
        $('input:checkbox, input:radio').each(function(){
            if (this.title === 'RQlevel' + src) {
                this.checked = false;
            }
        });
    }
}
请注意,应该这样称呼它:

$('#yourCheckbox').click(levels);

还要注意,这是检查元素的标题是否为
RQlevel
,后面是原始单击元素的
src
值。如果这不是您想要的,请将
每次调用中的
src
替换为
this.src
,以检查当前元素的
src
值。

我想您对
的引用有误。如果我正确理解您的问题,这应该满足您的要求:

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”,而是值“2”http://example.com/2". 所以,你想用一个。请参阅我的jsfiddle示例

另外,我没有使用
onclick
属性来注册事件处理程序,而是使用jQuery的方法绑定处理程序。以下是更新后的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;
                }
            });
        }
    }); 
});

这有点不清楚,你能提供一个例子吗?这个的目的是什么:var$This=$(This);这看起来不错,但似乎不起作用。我已经做了一些诊断,但还没有弄清楚。下面是代码实现位置的链接。如果选中两个复选框,然后取消选中两个复选框中的第一个复选框,则2个复选框中的第二个复选框也应取消选中。VISIT::并单击初学者加载表单请在jsfiddle.net上创建一个示例。@Robin,我已经更新了我的答案,以解决jsfiddle中显示的问题。不,我所追求的你完全正确。但是,当我测试它时,我发现我无法取消选中该框,而该框也会取消选中另一个框。它只是不让我取消选中它。