如何用js检查两个单选按钮的HTML值?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (139)

我的HTML中有两个单选按钮。其中至少有一个必须是有价值的“是”。当用户复制第二个值时,我想检查值(如果是“,那么我们不在乎,如果它是”否“,那么我们必须检查第一个值是多少)。我试过使用这个JavaScript代码,但它不起作用。有人能帮我吗?

    <div class="form-row">
        <div class="form-group col-md-4">
            <label class="form-text">Show email.</label>
        </div>

        <div class="form-group col-md-8">
        <div class="btn-group btn-group-toggle" data-toggle="buttons">
            <label class="btn btn-secondary active">
                <input type="radio" name="email" id="yes" value="yes" autocomplete="off" checked="checked"> Yes
            </label>
            <label class="btn btn-secondary">
                <input type="radio" name="email" id="no" value="no" autocomplete="off"> No
            </label>
        </div>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group col-md-4">
            <label class="form-text">Show number.</label>
        </div>

        <div class="form-group col-md-8">
        <div class="btn-group btn-group-toggle" data-toggle="buttons">
            <label class="btn btn-secondary active">
                <input type="radio" name="number" id="yes" value="yes" autocomplete="off" checked="checked"> Yes
            </label>
            <label class="btn btn-secondary">
                <input type="radio" name="number" id="no" value="no" autocomplete="off" onclick="check(this)"> No
            </label>
        </div>
        </div>
    </div>

        <script language='javascript' type='text/javascript'>
            function check(input) {
                if (document.getElementById('email').value == "no" && document.getElementById('number').value == "no") 
                {
                    input.setCustomValidity('One must be yes.');
                } else {
                    // input is valid -- reset the error message
                    input.setCustomValidity('');
                        }
                }
        </script>
提问于
用户回答回答于

当前,您没有具有id“电子邮件”,也不包括任何带有id“号码”。你应该给出你正在使用的两个单选按钮:

 <div class="form-row">
        <div class="form-group col-md-4">
            <label class="form-text">Show email.</label>
        </div>

        <div class="form-group col-md-8">
        <div class="btn-group btn-group-toggle" data-toggle="buttons">
            <label class="btn btn-secondary active">
                <input type="radio" name="email" id="showemailyes" value="yes" autocomplete="off" checked="checked"> Yes
            </label>
            <label class="btn btn-secondary">
                <input type="radio" name="email" id="showemailno" value="no" autocomplete="off"> No
            </label>
        </div>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group col-md-4">
            <label class="form-text">Show number.</label>
        </div>

        <div class="form-group col-md-8">
        <div class="btn-group btn-group-toggle" data-toggle="buttons">
            <label class="btn btn-secondary active">
                <input type="radio" name="number" id="shownumberyes" value="yes" autocomplete="off" checked="checked"> Yes
            </label>
            <label class="btn btn-secondary">
                <input type="radio" name="number" id="shownumberno" value="no" autocomplete="off" onclick="check(this)"> No
            </label>
        </div>
        </div>
    </div>

        <script language='javascript' type='text/javascript'>
            function check(input) {
                if (document.getElementById('showemailno').checked && document.getElementById('shownumberno').checked) 
                {
                   alert("One must be yes.");
                    input.setCustomValidity('One must be yes.');
                } else {
                    // input is valid -- reset the error message
                    input.setCustomValidity('');
                        }
                }
        </script>

用户回答回答于

我想像这样的事情可能会奏效:

        <label class="btn btn-secondary active">
            <input type="radio" name="email" id="firstRadioTrue" value="yes" autocomplete="off" checked="checked"> Yes
        </label>
        <label class="btn btn-secondary">
            <input type="radio" name="email" id="firstRadioFalse" value="no" autocomplete="off"> No
        </label> 
        <label class="btn btn-secondary active">
            <input type="radio" name="number" id="secondRadioTrue" value="yes" autocomplete="off" checked="checked"> Yes
        </label>
        <label class="btn btn-secondary">
            <input type="radio" name="number" id="secondRadioFalse" value="no" autocomplete="off"> No
        </label>



        <script language='javascript' type='text/javascript'>

        var radioTopOne = document.getElementById('firstRadioTrue');
        var radioTopTwo = document.getElementById('firstRadioFalse');

        var radioOne = document.getElementById('secondRadioTrue');
        var radioTwo = document.getElementById('secondRadioFalse');

        radioOne.addEventListener('click',function(){
            if(radioOne.checked == true && radioTopOne.checked == true){
                alert("BOTH TRUE");
            }
            else if(radioOne.checked == true && radioTopOne.checked == false){
                alert("TOP FALSE, BOT TRUE");
            };
        });
        radioTwo.addEventListener('click',function(){
            if(radioTwo.checked == true && radioTopTwo.checked == true){
                alert("BOTH FALSE");
            }
            else if(radioTwo.checked == true && radioTopTwo.checked == false){
                alert("BOT FALSE, TOP TRUE");
            }
        });




    </script>

扫码关注云+社区

领取腾讯云代金券