简体   繁体   English

jQuery检索单选旁边的文本(无标签)-可以单击,但如何加载?

[英]jquery retrieve text next to radio (no label tag) — have working on click but how to work on load?

http://jsfiddle.net/acts7/wcL7c/9/ So I have this code. http://jsfiddle.net/acts7/wcL7c/9/所以我有这段代码。

(Before you suggest... no I cannot wrap a label around the text. I don't have access to it) (在您建议之前……不,我不能在文本周围包裹标签。我无权访问它)

I am able to retrieve the text immediately following the radio input/button when the user clicks on it, OR when they focus on it. 当用户单击单选按钮或单选按钮时,或者当他们专注于单选按钮时,我可以立即检索该文本。 In other words when the user takes action. 换句话说,当用户采取行动时。

However; 然而; as I'm trying to make life better for those with screenreaders, I need to apply this text prior to clicking on it. 由于我正在努力为使用屏幕阅读器的用户提供更好的生活,因此需要在单击该文本之前先应用此文本。

How can I loop thru the entire form, grab the text immediately following each input field, and assign that to the title attribute on the radio button? 如何遍历整个表单,立即在每个输入字段之后抓取文本,并将其分配给单选按钮上的title属性?

QUESTION:: How can I get that text ... I already know how to assign it to the title attribute 问题:我如何获取该文本...我已经知道如何将其分配给title属性

THIS WORKS 这项工作

$("input[type=radio]").focus(function(e) {
    var radioText = e.currentTarget.nextSibling.data
    //alert(radioText);
     $(this).attr('title', radioText);
});

THIS does NOT 这不

$("input[type=radio]").each(function() {
    var radioText = $(this).currentTarget.nextSibling.data
    alert(radioText);
});

In the event handler, the currentTarget property of the event object is the element. 在事件处理程序中,事件对象的currentTarget属性是元素。 In the loop this is the element, so just use that instead of currentTarget : 在循环中, this是元素,因此只需使用它代替currentTarget

$("input[type=radio]").each(function() {
  $(this).attr('title', this.nextSibling.data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM