最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get label value from checkbox using jquery - Stack Overflow

programmeradmin0浏览0评论

Hello Everyone I want to capture label name(72*30) using the following code:

$("input[type = 'radio']:checked:last").each(function() {
  var idVal = $(this).attr("id");
  var a = $("label[for='"+idVal+"']").text();
  alert(a);
});
<script src=".1.1/jquery.min.js"></script>
<li class="radio">
  <input class="tmcp-field nav nav-pills dimension-layer-dimension  tm-epo-field tmcp-radio" type="radio">
  <label id="tmcp_radio_9" >72*30</label>	
  <span class="price tc-price  hidden">
    <span class="amount">20 <i class="fa fa-inr"></i></span>
  </span>		
</li>

Hello Everyone I want to capture label name(72*30) using the following code:

$("input[type = 'radio']:checked:last").each(function() {
  var idVal = $(this).attr("id");
  var a = $("label[for='"+idVal+"']").text();
  alert(a);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="radio">
  <input class="tmcp-field nav nav-pills dimension-layer-dimension  tm-epo-field tmcp-radio" type="radio">
  <label id="tmcp_radio_9" >72*30</label>	
  <span class="price tc-price  hidden">
    <span class="amount">20 <i class="fa fa-inr"></i></span>
  </span>		
</li>

But the code doesn't alert anything. What is wrong?

Share Improve this question edited Sep 30, 2016 at 9:40 rocambille 16k12 gold badges52 silver badges70 bronze badges asked Sep 30, 2016 at 8:43 techiva.blogspot.techiva.blogspot. 1,1903 gold badges18 silver badges38 bronze badges 3
  • you can directly get the value using id..? then why – Keerthivasan Commented Sep 30, 2016 at 8:45
  • 1 You have given an id to label and can access directly using $("#tmcp_radio_9").text() – Lovepreet Singh Commented Sep 30, 2016 at 8:46
  • @KeerthiVasan i have three radio button id is generating dynamically same – techiva.blogspot. Commented Sep 30, 2016 at 8:48
Add a ment  | 

3 Answers 3

Reset to default 6

You can use next() because your label is after (next) your input

$("input[type='radio']:checked:last").next().text()

Hope this will help you.

You can see demo here

Use this keyword

$("input[type='radio']:checked").each(function() {

   var a= $(this).next().text();
   alert(a);

});

Try this also

$("input[type='radio']:checked").each(function() {

   var a= $(this).closest("label").text();
   alert(a);

});
发布评论

评论列表(0)

  1. 暂无评论