JQuery 使用 title 和 src 属性
我正在执行一个脚本,该脚本将通过单击复选框来获取输入的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您对
this
的引用感到困惑。如果我正确理解您的问题,这应该可以满足您的要求:更新:
我应该早点意识到这一点,但您的主要问题是使用
src
属性作为基础你的比较。src
属性被解析,以便当您查询它时,该值将是绝对 URL。也就是说,您将拥有值“http://example.com/2”,而不是值“2”。因此,您想使用 数据属性。请参阅我对 jsfiddle 的更新以获取工作示例。另外,我没有使用
onclick
属性来注册事件处理程序,而是使用 jQuery 的 .bind() 方法。这是更新后的 JavaScript:I think you have your references to
this
confused. If I understand your question correctly, this should do what you are wanting:Update:
I should have realized this earlier, but your main problem is using the
src
attribute as the basis of your comparison. Thesrc
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:试试这个:
请注意,应该这样调用:
另请注意,这是检查元素的标题是否为
RQlevel
后跟原始的src
值单击的元素。如果这不是您想要的,请将each
调用中的src
替换为this.src
以检查src
> 当前元素的值。Try this:
Note that this should be called like this:
Note also that this is checking to see if the title of the element is
RQlevel
followed by thesrc
value of the original clicked element. If this is not what you want, replacesrc
in theeach
call withthis.src
to check for thesrc
value of the current element.