[英]how to get text of label that belongs to checked radio button
I'm trying to get the text of the labels that belong to checked radio buttons, but I can't and my code doesn't work correctly: 我正在尝试获取属于选中的单选按钮的标签的文本,但是我不能,并且我的代码无法正常工作:
$(document).ready(function() { var IS_SEX_Title $('input[name="IS_SEX"]:radio:checked').each(function() { IS_SEX_Title = $(this).parent().find('label').text(); }); alert(IS_SEX_Title) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="radio-inline" style="margin-right:10px;"> <input value="0" class="form-control psd-input-radio" name="IS_SEX" id="IS_SEX_0" data-top-id="IS_SEX" checked="" type="radio"> male </label>
Try something like this: 尝试这样的事情:
$('input[name="IS_SEX"]').parent('label').text();
It will return male
它将返回
male
The parent is the label
- remove find()
. 父级是
label
-remove find()
。 Also note that the each()
is redundant as you can only have one checked radio button per named group. 还要注意,
each()
是多余的,因为每个命名组只能有一个选中的单选按钮。
$(document).ready(function() { var IS_SEX_Title = $('input[name="IS_SEX"]:radio:checked').parent().text().trim(); console.log(IS_SEX_Title) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="radio-inline" style="margin-right:10px;"> <input value="0" class="form-control psd-input-radio" name="IS_SEX" id="IS_SEX_0" data-top-id="IS_SEX" checked="true" type="radio"> male </label> <label class="radio-inline" style="margin-right:10px;"> <input value="0" class="form-control psd-input-radio" name="IS_SEX" id="IS_SEX_1" data-top-id="IS_SEX" type="radio"> female </label>
Just add this $(this).parent().text();
只需添加此
$(this).parent().text();
instead of $(this).parent().find('label').text();
而不是
$(this).parent().find('label').text();
because label has text. 因为标签有文字。 so you don't need to find label.
因此您无需查找标签。
See working snippet below: 请参见下面的工作片段:
$(document).ready(function() { var IS_SEX_Title $('input[name="IS_SEX"]:radio:checked').each(function() { IS_SEX_Title = $(this).parent().text(); }); alert(IS_SEX_Title); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <label class="radio-inline" style="margin-right:10px;"> <input value="0" class="form-control psd-input-radio" name="IS_SEX" id="IS_SEX_0" data-top-id="IS_SEX" checked="" type="radio">male</label>
It's not working because you are changing the value inside forEach. 它不起作用,因为您正在更改forEach中的值。 Try put the alert inside the foreach then you can access directly by class.
尝试将警报放在foreach内,然后您可以按班级直接访问。 Take a look bellow:
看下面:
var IS_SEX_Title $('input[name="IS_SEX"]:radio:checked').each(function() { IS_SEX_Title = $('.radio-inline').text(); console.log(IS_SEX_Title); alert(IS_SEX_Title); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="radio-inline" style="margin-right:10px;"> <input value="0" class="form-control psd-input-radio" name="IS_SEX" id="IS_SEX_0" data-top-id="IS_SEX" checked="" type="radio"> male </label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.