Jquery获取radio单选按钮的value与后面的文字
一组单选按钮如图:
<input name="classId" value="8afa94f45ba3e2c1015ba3fac6c00000" type="radio">1班 <input name="classId" value="8afa94f45bacf46c015bacf860cd0000" type="radio">2班 <input name="classId" value="8afa94f45bacf46c015bacf8e7f60001" type="radio">3班 <input name="classId" value="8afa94f45bacf46c015bacf914be0002" type="radio">4班 <input name="classId" value="402881905bcbdb30015bcbf3c4a10005" type="radio">5班
我们希望获取的是:value值”8afa94f45ba31dce015ba32432570001” 与 文字:“1班”
获取代码如下:
var value= $("input[name='classId']:checked").val(); var radioName = $("input[name='classId']:checked")[0].nextSibling.nodeValue;
另一种写法:
(1)
若html为:<input type="radio" id="test" name=“test” value="8afa94f45ba31dce015ba32432570001" /><span>1班</span>
js为:$("input[name='
test
']:checked").next("span").text()
(2)
若html为:<input type="radio" id="
test
" name="
test
" value="8afa94f45ba31dce015ba32432570001" /> <label for="
test
">2班</label>
js为:$("input[name='test']:checked").next("label").text()