如何在 JQuery 中获取复选框文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17072375/
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
How to get check box text in JQuery
提问by Ragavan
<input type="checkbox" name="vehicle" >Car</input>
How to get text in JQuery ?
如何在 JQuery 中获取文本?
not needed any values for checkbox how to get textarea in alert ?
不需要复选框的任何值如何在警报中获取文本区域?
回答by PSL
Input should not contain any contents. You can do this way:-
输入不应包含任何内容。你可以这样做:-
Markup
标记
<input type="checkbox" name="vehicle" /><label>Car</label>
Script
脚本
$(':checkbox[name=vehicle]').next('label').text();
if you are using it in change event: then
如果您在更改事件中使用它:那么
$(yourcheckbox).on('change', function(){
var mytext = $(this).next('label').text();
})
Have a read from W3on the standard way to use label for inputs.
阅读W3中使用标签进行输入的标准方法。
回答by tymeJV
I would put the text after the input
into a label
element and get it that way:
我会将在 之后的文本input
放入一个label
元素并以这种方式获取:
<input type="checkbox" name="vehicle" />
<label>Car</label>
JS:
JS:
$("input[type='checkbox']").change(function() {
console.log($(this).next("label").text());
});
回答by dezman
var text = $('input[type="checkbox"]').text();
alert(text);
Is this what you wanted?
这是你想要的吗?
But really you meant:
但实际上你的意思是:
<p>Car<input type="checkbox" name="vehicle"></p>
JS:
JS:
var text = $('input[type="checkbox"]').parent().text();
alert(text);