使用 Jquery 获取复选框的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5518345/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:17:18  来源:igfitidea点击:

Get Text of checkbox with Jquery

jqueryhtml

提问by slandau

<input type="checkbox" name="PrePayment">Pre-Payment<br />

How would I get the text "Pre-Payment" from this using Jquery?

我如何使用 Jquery 从中获取文本“预付款”?

Thanks!

谢谢!

采纳答案by downed

You should probably have a value attribute in the checkbox.

您可能应该在复选框中有一个 value 属性。

<input type="checkbox" name="PrePayment" value="Pre-Payment">Pre-Payment<br />

Then you can simply use the attr command:

然后你可以简单地使用 attr 命令:

$(input).attr('value');

回答by mu is too short

I'd recommend putting the text inside a <label>tag so that you could click on it (and so that screen readers and such could make sense of your form):

我建议将文本放在<label>标签中,以便您可以单击它(并且屏幕阅读器等可以理解您的表单):

<input type="checkbox" name="PrePayment" id="pre-payment">
<label for="pre-payment">Pre-Payment</label>
<br />

Then, the whole thing becomes easy:

然后,整个事情就变得简单了:

var text    = $('label[for=pre-payment]').text();
var or_this = $('#pre-payment').next('label').text();

I'd prefer the first option, label[for=...], as it is less fragile than the second

我更喜欢第一个选项,label[for=...]因为它不如第二个选项脆弱

回答by karim79

Maybe:

也许:

$("input[name='PrePayment']")[0].nextSibling.nodeValue;

Try it here.

在这里试试。

回答by Chandresh

Check the below example and you get the script how to get label of checkbox in jQuery

检查下面的示例,您将获得如何在 jQuery 中获取复选框标签的脚本

<html>
 <body>
     <ul>
         <li>
             <input type="checkbox" name="mouse" value="1" id="mouse"  /><label for="mouse">Mouse</label>
            </li>
            <li>
             <input type="checkbox" name="keyboard" value="1" id="keyboard"  /><label for="mouse">Keyboard</label>
            </li>
            <li>
             <input type="checkbox" name="monitor" value="1" id="monitor"  /><label for="mouse">Monitor</label>
            </li>
        </ul>
    </body>
</html>

<script type="text/javascript">
 var label = jQuery('label[for=' + jQuery(this).attr('id') + ']').html();
 alert(label);
</script> 

Code taken from this link : http://chandreshrana.blogspot.in/2015/09/how-to-get-checkbox-label-text-jquery.html

取自此链接的代码:http: //chandreshrana.blogspot.in/2015/09/how-to-get-checkbox-label-text-jquery.html

回答by Alex Ward

I had a similar problem to this - another way you could make this work (with HTML5) is to use data attributes.

我有一个类似的问题 - 另一种可以使这项工作(使用 HTML5)工作的方法是使用数据属性。

As an example, you'd set a data attribute as follows:

例如,您可以按如下方式设置数据属性:

<input type="checkbox" name="PrePayment" data-text="Pre-Payment">Pre-Payment<br />

You could then read this data attribute by using the following jQuery:

然后,您可以使用以下 jQuery 读取此数据属性:

$(input).data('text');

The benefit of doing it this way is that you can have a seperate value parameter if required.

这样做的好处是,如果需要,您可以有一个单独的值参数。