2012-01-09 61 views
1

我有这样的代码获取文本与jQuery

<input type="checkbox"/> 
<span>Foobar</span> 
<input type="radio" checked="checked">Foo 
<input type="radio">Bar 

如何获取文本Foo使用jQuery其中$(this)是节点<input type="checkbox"/>
我可以通过$(this).parent().find(":checked").not($(this))得到<input type="radio" checked="checked">,但我不知道如何获得文本。

+0

这是不正确的HTML,所以你可能有麻烦 – 2012-01-09 14:35:32

+0

@ m.edmondson:什么是不正确的HTML? – 2012-01-09 14:36:30

+0

没有关闭它们应该是''Foo或' Foo'还是我误会了? – 2012-01-09 14:37:43

回答

1

一旦你达到目标的元素...

  • 使用[0]得到的DOM元素从jQuery对象中的第一个指数,

  • 使用nextSibling来获取文本节点,使用data来提取文本内容。


var txt = $(this).next().next()[0].nextSibling.data 

var txt = $(this).nextAll(':radio')[0].nextSibling.data 
+1

Ahaha,再次。只是输入这个;) – karim79 2012-01-09 14:36:09

+1

我想要选中的单选按钮的文本,所以我通过':checked'' $(this).nextAll(':checked')[0] .nextSibling.data'来更改':radio'。 谢谢 – Snote 2012-01-09 15:06:27

0

它看起来像.val()可能工作(API Reference)。

$(this).parent().find(":checked").not($(this)).val()