简体   繁体   English

如何检查单选按钮是否被选中?

[英]How can I check if a radio button is selected?

Hi I am trying to make a form, where are the fields are required, including one of two radio buttons, but for some reason the.checked property will not work neither if I am selecting it like this document.querySelector('input[name="radio"]:checked')... What am I not seeing?您好我正在尝试制作一个表单,其中需要字段,包括两个单选按钮之一,但由于某种原因,如果我像这样选择它, the.checked 属性将不起作用 document.querySelector('input[name ="radio"]:checked')...我没看到什么?

As you can see I was trying different approaches but something is missing如您所见,我正在尝试不同的方法,但缺少一些东西

 let contactForm = document.querySelector('.contact-form'); let firstName = document.getElementById('first-name'); let lastName = document.getElementById('last-name'); let textarea = document.getElementById('message'); // let radioBtn = document.querySelector('input[name="radio"]:checked'); // let firstRadioBtn = document.getElementById('male').checked; // let secondRadioBtn = document.getElementById('female').checked; // console.log(firstRadioBtn.value, secondRadioBtn.value); let submitBtn = document.querySelector('.btn'); let successMsg = document.querySelector('.success-message'); // let radios = document.getElementsByName('radio'); // let formValid = false; // console.log(radios) // for (let i = 0; i < radios.length; i++) { // if (radios[i].checked) { // formValid = true // } // } function checkInputs() { let firstNameInput = firstName.value; let lastNameInput = lastName.value; let text = textarea.value; if (firstNameInput === '' || lastNameInput === '' || text === '') { firstName.className = 'invalid'; lastName.className = 'invalid'; textarea.className = 'invalid'; successMsg.style.backgroundColor = '#e63946'; successMsg.style.visibility = 'visible'; successMsg.innerText = `Please fill in all the required fields` } if (firstNameInput.== '' && lastNameInput;== '' && text.== '') { firstName;className = 'valid'. lastName;className = 'valid'. textarea.className = 'valid'; successMsg.style.backgroundColor = '#52b788'; successMsg.style.visibility = 'visible'. successMsg.innerText = `Thank you for your message ${firstNameInput} ${lastNameInput}` } console:log(`${firstName.value} ${lastName:value} \n Gender. ${radios.value} \n Message: ${message.value}`) }
 <div class="radio-btn label"> <label for="radio">Male</label> <input type="radio" name="radio" id="male" value="male"> <label for="radio">Female</label> <input type="radio" name="radio" id="female" value="female"> </div>

This should work!这应该工作!

<div class="radio-btn label">
    <label for="radio">Male</label>
    <input type="radio" name="radio" id="male" value="male">
    <label for="radio">Female</label>
    <input type="radio" name="radio" id="female" value="female">
</div>
<div id="gender-error" style="color: red; display: none">Please select the gender</div>
<button type="submit" onclick="return submit();">Submit</button>

<script>
    function submit() {
        var radios = document.getElementsByName("radio");
        var value = ""
        for (var i = 0, length = radios.length; i < length; i++) {
            if (radios[i].checked) {
                value = radios[i].value;
                break;
            }
        }

        document.getElementById("gender-error").style.display = !value ? "block" : "none";
        alert(value ? "selected " + value : "no value selected");
    }
</script>

Output: Output:

在此处输入图像描述

在此处输入图像描述

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

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