Javascript Jquery 获取选中的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2712935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery get selected checkboxes
提问by kevin
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
嗨,我想获取页面中选定复选框的列表,实际上我真正需要的是获取复选框旁边元素的文本,这是一个 html 元素<li>,代码在下面,但它不起作用
Here is my current jQuery:
这是我当前的 jQuery:
$(document).ready(function () {
$('#target').click(function () {
alert("in");
var checkValues = [];
$('input[name=checkboxlist]:checked').each(function() {
alert($(this)val());
checkValues.push($(this)val());
});
});
});
Here is the HTML:
这是 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>
回答by Nick Craver
Your inputs need to be closed with a />on the end to make your HTML valid, like this:
您的输入需要/>在末尾以a结束以使您的 HTML 有效,如下所示:
<input type="checkbox" name="checkboxlist" />
Then you can do jQuery like this to get an array of the text:
然后你可以像这样执行 jQuery 来获取文本数组:
$(function () {
$('#target').click(function () {
var checkValues = $('input[name=checkboxlist]:checked').map(function() {
return $(this).parent().text();
}).get();
//do something with your checkValues array
});
});?
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.
回答by Andrew dh
$("input[type=checkbox]:checked").parent().text()
回答by rahul
First of all you have to close your checkbox tag
首先你必须关闭你的复选框标签
<input type="checkbox" name="checkboxlist"
should be
应该
<input type="checkbox" name="checkboxlist" />
I think, from your HTML you can get the text of the lis which will be the desired text
我认为,从您的 HTML 中,您可以获得 lis 的文本,这将是所需的文本
$(function(){
$('#target').click(function(){
var selected = new Array();
$("input:checkbox[name=checkboxlist]:checked").each(function(){
var val = $(this).closest("li").text();
selected.push(val);
});
});
});
回答by Technowise
You are not closing your input tags correctly. Also, $(this)val()should be $(this).val().
您没有正确关闭输入标签。另外,$(this)val()应该是$(this).val()。
If you intend to obtain the text of li element which contains the input element, you can write as below:
如果要获取包含 input 元素的 li 元素的文本,可以这样写:
$(this).parents("li:first").text();

