label中使用for连接input的id该如何根据input获取lable的text,两种方式均可获取对应的text,亲测有效!!!
html页面:
<div class="radio-custom radio-primary radio-inline">
<input data-type="radio" type="radio" class="" name="functionTypeId" id="functionTypeId_1" value="1" data-rule-required="false" checked="checked">
<label for="functionTypeId_1">打篮球</label>
</div>
<div class="radio-custom radio-primary radio-inline">
<input data-type="radio" type="radio" class="" name="functionTypeId" id="functionTypeId_2" value="2" data-rule-required="false">
<label for="functionTypeId_2">打游戏</label>
</div>
jquery中获取label值:两种方式
(1)使用next:
var functionTypeText = $(‘input:radio[name=functionTypeId]:checked‘).next("label").text();
(2)使用拼接:
var functionTypeText =$(‘label[for=‘+ "functionTypeId_" + $serializeData.functionTypeId +‘]‘).text();//($serializeData.functionTypeId 这是通过form表单获取的选中的单选框的value)
var applyBookWriteTypeText =$(‘label[for=‘+ "declarationFillId_" + $serializeData.declarationFillId +‘]‘).text();
label中使用for关联input的id,该如何根据input中的id获取lable的text
原文:https://www.cnblogs.com/yigeshaozijava/p/jquery20210719.html