有没有一种方法可以<label>
使用jquery从元素中获取文本?因为我们可以使用从中获取值,$(this).val();
但无法对标签文本执行相同的操作。
似乎我们应该能够<label>
通过$(this)
选择单选按钮来查询文本,因为$(this)
在此期间它知道其值。
最初带有case语句,并手动填写一些文本,但是如果我们能够只插入标签文本,则将不需要使用所有case语句。
想法/建议?
html
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#accordion-second">What You Are Interested In Learning About</a>
</h4>
</div>
<div id="accordion-second" class="panel-collapse collapse">
<div class="panel-body">
<div class="radio">
<label><input type="radio" name="optradio2" value="2a">the Automotive Industry</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio2" value="2b">the sports industry</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio2" value="2c">the music industry</label>
</div>
</div>
</div>
</div>
jQuery的
<script>
$('[name="optradio2"]').on('change', function () {
$('#accordion-third').slideToggle();
$('#accordion-second').slideToggle();
//how do you get the label text
var labelText = $("label", this).text();
//current method but would prefer to just insert the label text
/*var value = $(this).val();
switch (value) {
case "2a":
$('#blank2', $('#myModal')).text('the automotive industry');
break;
case "2b":
$('#blank2', $('#myModal')).text('the sports industry');
break;
case "2c":
$('#blank2', $('#myModal')).text('the music industry');
break;
default:
alert("oops! something went wrong");
}*/
})
</script>
上下文选择器使用find
并搜索后代。
标签不是输入的子元素,而是相反的,因此您必须寻找父母,并且closest
看起来很合适
var labelText = $(this).closest("label").text();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句