Jquery获得选中的复选框 [英] Jquery get selected checkboxes

查看:83
本文介绍了Jquery获得选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在页面中获取所选复选框的列表,实际上我真正需要的是获取复选框旁边元素的文本,这是一个html元素< li>
代码在下方且无法正常工作

Hi I'd like to get the list of selected checkboxes ina page, actually what I really need is th get the text of the element next to the checkbox which is a html element <li> the code is below and its not working

这是我目前的jQuery:

Here is my current jQuery:

$(document).ready(function () {
  $('#target').click(function () {
    alert("in");
    var checkValues = [];
    $('input[name=checkboxlist]:checked').each(function() {
      alert($(this)val());
      checkValues.push($(this)val());
    });
  });
});

这是HTML:

<ul id="7b1fe2bd-1b26-4185-8cd9-aec10e652a70">
 <li>Structured Products<input type="checkbox" name="checkboxlist"</li>
 <li>Global FID
 <ul>
  <li>PowerPoint Presentations<input type="checkbox" name="checkboxlist"</li>
  <li>Global Deck
  <ul>
   <li>Test1<input type="checkbox" name="checkboxlist"</li>

   <li>Test2<input type="checkbox" name="checkboxlist"</li>
   <li>Test3<input type="checkbox" name="checkboxlist"</li>

  </ul>
  <input type="checkbox" name="checkboxlist"</li>
  <li>Credit Default Swaps Position
  <ul>
   <li>Test4<input type="checkbox" name="checkboxlist"</li>

   <li>Test5<input type="checkbox" name="checkboxlist"</li>

  </ul>
  <input type="checkbox" name="checkboxlist"</li>
  <li>Thought Leadership<input type="checkbox" name="checkboxlist"</li>
  <li>Fixed Income Perspectives<input type="checkbox" name="checkboxlist"</li>
  <li>Public Policy Information and Regulatory<input type="checkbox" name="checkboxlist"</li>

  <li>Regional FID<input type="checkbox" name="checkboxlist"</li>

 </ul>
 <input type="checkbox" name="checkboxlist"</li>
 <li>Global Rates<input type="checkbox" name="checkboxlist"</li>
 <li>Global Credit Products<input type="checkbox" name="checkboxlist"</li>
 <li>FX<input type="checkbox" name="checkboxlist"</li>

 <li>Emerging Markets<input type="checkbox" name="checkboxlist"</li>
 <li>Commodities<input type="checkbox" name="checkboxlist"</li>
 <li>testcat<input type="checkbox" name="checkboxlist"</li>
 <li>testcat<input type="checkbox" name="checkboxlist"</li>

</ul>


推荐答案

您的输入需要以<$ c结束$ c> /> 最后使您的HTML有效,如下所示:

Your inputs need to be closed with a /> on the end to make your HTML valid, like this:

<input type="checkbox" name="checkboxlist" />

然后你可以像这样做jQuery来得到一个文本数组:

Then you can do jQuery like this to get an array of the text:

$(function () {
  $('#target').click(function () {
    var checkValues = $('input[name=checkboxlist]:checked').map(function() {
        return $(this).parent().text();
    }).get();
    //do something with your checkValues array
  });
});​

您可以在此处看到此工作

因为你需要的只是父母的 .text() 使用您的布局,您可以使用 .map() 根据选择器快速获取属性数组。

Since all you need is the parent's .text() with your layout, you can use .map() to quickly get an array of a property based on the selector.

这篇关于Jquery获得选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆