Javascript 如何使用JQuery检查单选按钮?
我在一个组中有两个单选按钮,我想使用JQuery检查单选按钮是否已选中,如何检查?给定一组单选按钮:Javascript 如何使用JQuery检查单选按钮?,javascript,jquery,radio-button,Javascript,Jquery,Radio Button,我在一个组中有两个单选按钮,我想使用JQuery检查单选按钮是否已选中,如何检查?给定一组单选按钮: //the following code checks if your radio button having name like 'yourRadioName' //is checked or not $(document).ready(function() { if($("input:radio[name='yourRadioName']").is(":checked")) {
//the following code checks if your radio button having name like 'yourRadioName'
//is checked or not
$(document).ready(function() {
if($("input:radio[name='yourRadioName']").is(":checked")) {
//its checked
}
});
<input type="radio" id="radio1" name="radioGroup" value="1">
<input type="radio" id="radio2" name="radioGroup" value="2">
您可以按如下方式获取组中当前选中的值:
if ($("#radio1").prop("checked")) {
// do something
}
// OR
if ($("#radio1").is(":checked")) {
// do something
}
// OR if you don't have ids set you can go by group name and value
// (basically you need a selector that lets you specify the particular input)
if ($("input[name='radioGroup'][value='1']").prop("checked"))
$("input[name='radioGroup']:checked").val()
也来看看这个:
$(document).ready(function() {
if($("input:radio[name='yourRadioGroupName'][value='yourvalue']").is(":checked")) {
//its checked
}
});
这是最佳实践
$("input[name='radioGroup']:checked").val()
进一步了解一些答案-如果您执行以下操作,您可以检查收音机组中的任何元件是否已检查:
if($('input[name=“yourRadioNames”]:checked').val(){
(checked)或if(!$('input[name=“yourRadioNames”]:checked').val(){
(未选中)单选按钮是
<input type="radio" id="radio_1" class="radioButtons" name="radioButton" value="1">
<input type="radio" id="radio_2" class="radioButtons" name="radioButton" value="2">
jQuery 3.3.1
if (typeof $("input[name='yourRadioName']:checked").val() === "undefined") {
alert('is not selected');
}else{
alert('is selected');
}
试试这个:
var count =0;
$('input[name="radioGroup"]').each(function(){
if (this.checked)
{
count++;
}
});
如果选中任何一个单选按钮,您将得到1个可能重复的可能重复的可能重复的上述注释中提到的问题是询问如何获取选中的值和检查已选择的特定值,而不是验证是否选中。因此,这是不同的。描述您的代码实际执行的操作会有所帮助(针对提问者——不是我)
var count =0;
$('input[name="radioGroup"]').each(function(){
if (this.checked)
{
count++;
}
});