简体   繁体   English

如何使用jquery从输入复选框中检索文本?

[英]How do I retrieve text from an input checkbox with jquery?

I'm trying to retrieve the text for selected check boxes like so: 我正在尝试检索所选复选框的文本,如下所示:

HTML: HTML:

<label class="checkbox">        
    <input type="checkbox" name="priority" value="2" checked="checked">2 - Critical
</label>
<label class="checkbox">        
    <input type="checkbox" name="priority" value="3">3 - Important
</label>

jquery: jQuery的:

$('#priorityContents input:checkbox:checked').each(function() {
    if(priorityText.length > 0) {
        priorityText = priorityText + "|";
    }
    priorityText = priorityText + $(this).text();
});

alert(priorityText);

I would expect to see: 我希望看到:

2 - Critical

I don't get any errors in my console. 我的控制台中没有任何错误。 What am I missing? 我错过了什么?

You can try: 你可以试试:

<input id="cb" type="checkbox" name="priority" value="2" checked="checked">
<label for='cb' class="checkbox"> 2 - Critical</label>

$('#priorityContents input[type="checkbox"]:checked').each(function() {
    var txt = $(this).next('label').text();
});

please note that :checkbox selector is deprecated you can use input[type="checkbox"] instead. 请注意:checkbox 不建议使用:checkbox选择器,您可以使用input[type="checkbox"]代替。

Based on the code you posted, why would you expect to see that result? 根据您发布的代码,您为什么期望看到该结果? At no point in that code have you attempted to retrieve the text. 在该代码中,您没有尝试检索文本。 I'd suggest: 我建议:

$('#priorityContents input:checkbox:checked').each(function() {
    var next = this.nextSibling,
        text = next.nodeType == 3 ? next.nodeValue : '';
    console.log(text);
});

JS Fiddle demo . JS小提琴演示

This iterates over each checked checkbox within the element of the given id , looks at the next sibling of the current node (not the jQuery object, the plain DOM node) and, if that node is a textNode (a node of nodeType equal to 3 ) assigns the nodeValue (the text contents of that node) to the variable. 这将迭代给定id的元素中的每个已检查复选框,查看当前节点的下一个兄弟(不是jQuery对象,普通DOM节点),如果该节点是textNodenodeType等于3的节点) )将nodeValue (该节点的文本内容)分配给变量。

If it's not a textNode , then it assigns an empty string instead. 如果它不是textNode ,则它会分配一个空字符串。

You want to get to the label element, which is the parent of the input : 您想要到达label元素,它是input的父元素:

$('#priorityContents input[type="checkbox"]:checked').parent();

Here's the fiddle: http://jsfiddle.net/Hk63N/ 这是小提琴: http//jsfiddle.net/Hk63N/


For increased performance, you should consider splitting up the selector: 为了提高性能,您应该考虑拆分选择器:

var priorityText = '';

$('#priorityContents input[type="checkbox"]').filter(':checked').parent().each(function() {
    if ( ! priorityText ) {
        priorityText = priorityText + "|";
    }
    priorityText = priorityText + $(this).text();
});

alert(priorityText);​

From the jQuery docs : 来自jQuery文档

To achieve the best performance when using these selectors, first select some elements using a pure CSS selector, then use .filter(). 要在使用这些选择器时获得最佳性能,首先使用纯CSS选择器选择一些元素,然后使用.filter()。

Here's the fiddle for this: http://jsfiddle.net/Hk63N/1/ 这是这个小提琴: http//jsfiddle.net/Hk63N/1/

First, wrap the text only in the <label> tags, which is a good idea anyway for usability. 首先,将文本仅包装在<label>标签中,这对于可用性来说是个好主意。 Assign the for attribute to the ID of the checkbox: for属性分配给复选框的ID:

<input type="checkbox" name="priority" id="priority-2" value="2" checked="checked">
<label class="checkbox" for="priority-2">        2 - Critical</label>

Then you can target this easily with jQuery selectors: 然后,您可以使用jQuery选择器轻松地定位:

$('#priorityContents input:checkbox:checked').each(function() {
    priorityText = $('label[for="'+$(this).attr('id')+'"]').text();
    ...
});

That said, the easiest approach might instead be to add whatever text you want in a data-prioritytext attribute on the checkbox, and extract that with .data('prioritytext') in your code. 也就是说,最简单的方法可能是在复选框的data-prioritytext属性中添加您想要的任何文本,并在代码中使用.data('prioritytext')提取。

There's some stuff definitely missing from your code. 您的代码中肯定缺少一些东西。 Like the #priorityContents elements which is pretty important if you're searching for it. 就像#priorityContents元素一样,如果你正在搜索它,这非常重要。

Anyway I created this demo that works for me. 无论如何,我创建了这个适合我的演示。 Basically what you have wrong I believe is this part: 基本上你错了我相信这部分:

priorityText = priorityText + $(this).text();

The actual checkbox element doesn't own the .text(). 实际的checkbox元素不拥有.text()。 YOu needt to go up to the parent to get the actual value contained in there. 你需要去父母那里获得那里包含的实际价值。

DEMO DEMO

Try this 尝试这个

var checkedTxt=$('.checkbox :checked').parent().text();
console.log(checkedTxt);

DEMO. DEMO。

var name=$(this).parent().text();

you will get the text of that checkbox In html checkbox have no attributes to take text data. 你将获得该复选框的文本在html复选框中没有属性来获取文本数据。 so use parent() function to take 所以使用parent()函数

您可以使用jquery以这种方式检索已选中复选框的文本。

var value = $(document).find('input[type="checkbox"]:checked').attr('value');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM