JQuery 使用 title 和 src 属性

发布于 2024-10-14 21:03:32 字数 499 浏览 2 评论 0原文

我正在执行一个脚本,该脚本将通过单击复选框来获取输入的 src 属性,然后遍历所有其他复选框和单选按钮 .each 并删除任何输入(即 title 属性)的选中属性是'RQlevel' + this.src。希望这已经足够清楚了。

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

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

工作示例位于 http://jsfiddle.net/V8FeW/

I am after a script that will onclick of a checkbox will get the src attribute of the input and then go through all the other checkboxes and radio buttons .each and remove the checked attribute of any inputs thats title attribute is is 'RQlevel' + this.src. Hope that is clear enough.

Here is my attempt, however it is not correct.

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

Working example at http://jsfiddle.net/V8FeW/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三岁铭 2024-10-21 21:03:32

我认为您对 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。也就是说,您将拥有值“http://example.com/2”,而不是值“2”。因此,您想使用 数据属性。请参阅我对 jsfiddle 的更新以获取工作示例。

另外,我没有使用 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;
                }
            });
        }
    }); 
});

I think you have your references to this confused. If I understand your question correctly, this should do what you are wanting:

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');
      }
    });
  }
}

Update:

I should have realized this earlier, but your main problem is using the src attribute as the basis of your comparison. The src attribute gets parsed so that when you query it the value will be the absolute URL. That is, instead of the value "2" you would have the value "http://example.com/2". So, you want to use a data attribute. See my update of your jsfiddle for a working example.

Also, instead of using the onclick attribute to register the event handler, I bound the handler with jQuery's .bind() method. Here is the updated 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;
                }
            });
        }
    }); 
});
作妖 2024-10-21 21:03:32

试试这个:

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 值单击的元素。如果这不是您想要的,请将 each 调用中的 src 替换为 this.src 以检查 src > 当前元素的值。

Try this:

function levels() {
    if (!this.checked) {
        var src = this.src;
        $('input:checkbox, input:radio').each(function(){
            if (this.title === 'RQlevel' + src) {
                this.checked = false;
            }
        });
    }
}

Note that this should be called like this:

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

Note also that this is checking to see if the title of the element is RQlevel followed by the src value of the original clicked element. If this is not what you want, replace src in the each call with this.src to check for the src value of the current element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文