与JQuery的标题和src属性工作 [英] JQuery working with title and src attributes

查看:180
本文介绍了与JQuery的标题和src属性工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个脚本,将一个的onclick复选框将得到输入的src属性,然后再通过其他所有的复选框和单选按钮后。每个,然后取出任何投入这就是title属性是选中的属性是'的RqLevel'+ this.src 。希望是非常明显的。

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

例如工作在 http://jsfiddle.net/V8FeW/

推荐答案

我认为你必须您引用本困惑。如果我没有理解你的问题,这应该做你想要什么:

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

更新:

我应该意识到这个较早,但你的主要问题是使用的src 属性作为比较的基础。在的src 属性被解析,这样,当你查询它的价值将是绝对URL。也就是说,代替的值2你将具有值http://example.com/2。所以,你需要使用<一个href=\"http://www.w3.org/TR/html5/elements.html#embedding-custom-non-visible-data-with-the-data-attributes\"相对=nofollow>数据属性。请参阅您的jsfiddle我更新,了解工作的例子。

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.

此外,而不是使用的onclick 属性,来注册事件处理程序,我绑定的处理程序与jQuery的的。bind()的方法。以下是更新的JavaScript:

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

这篇关于与JQuery的标题和src属性工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆