简体   繁体   English

如何使用JavaScript验证HTML中的单选按钮?

[英]How to validate radio button in html using Javascript?

how can i validate the input of radio button? 如何验证单选按钮的输入? Currently,i doing a survey form using radio button. 目前,我正在使用单选按钮制作调查表。 I have try many way to validate the radio but all the code are unusable. 我尝试了许多方法来验证无线电,但所有代码均无法使用。 Anyone can help?TQ. 有人可以帮忙吗? Below is my code 下面是我的代码

 var f = document.frmSubmit; var radios = document.getElementsByName('appbk') for (var i = 0; i < radios.length; i++) { if (document.getElementById("appbk") == false) { alert('Remarks has exceeded allowable radio characters.'); f.appbk.focus(); return false; // checked } } if (f.FEEDBACK_MSG.value != "" ) { if (f.FEEDBACK_MSG.value.length > 500 ){ bootbox.alert('Remarks has exceeded allowable maximum characters.'); f.FEEDBACK_MSG.focus(); return false; } } return true; document.getElementById("btnsubmit").disabled = true; } 
 <tr> <td><div class="text2">Appointment Booking </div></td> <td><div class="text3"> <span style="margin:0 30px;"><input type="radio" id="appbk1" name="appbk" value="1"><lable>1</lable></span> <span style="margin:0 30px;"><input type="radio" id="appbk2" name="appbk" value="2"><lable>2</lable></span> <span style="margin:0 30px;"><input type="radio" id="appbk3" name="appbk" value="3"><lable>3</lable></span> <span style="margin:0 30px;"><input type="radio" id="appbk4" name="appbk" value="4"><lable>4</lable></span> <span style="margin:0 30px;"><input type="radio" id="appbk5" name="appbk" value="5"><lable>5</lable></span> </div> </td> </tr> 

id应该是唯一的,您可以使用document.getElementById('单选按钮的id')。checked来设置单选按钮的值(是或否)。也可以像radios [i] .checked == false这样的数组单选代替文档。 getElementById(“ appbk”)==否

This depends on when you want to validate, however, on the click of your forms submit button, I would do a function onclick for the button. 这取决于您何时要验证,但是,在单击表单提交按钮时,我将为按钮执行onclick函数。 If any field checks aren't selected (using a for loop and boolean set to true on check), then prevent the default button action of a href and instead display a div with an error message in. This way, the form will also keep its previous data. 如果未选择任何字段检查(在检查时使用for循环并将boolean设置为true),则阻止href的默认按钮操作,而是显示带有错误消息的div。这样,表单也将保留它以前的数据。

try this.... jsfiddle 试试这个...。 jsfiddle

$('.submit').click(function(e){
if($('input[name="myRadio"]:checked', '#myForm').val()!==undefined){
alert('submited');
}
else{
alert('enter your choice');
}
});

You can check the link posted by RAYON DABRE as his code is working perfectly. 您可以检查RAYON DABRE发布的链接,因为他的代码运行正常。

OR 要么

You just have to check the records selected or not based on your requirement. 您只需要根据需要检查选择的记录或不检查。 A sample code has been added for your reference 已添加示例代码供您参考

function funcheckvalidate() {
        if (document.getElementById('btnSubmit').checked == true) {
            alert('button checked');
        } else if (document.getElementById('btnSubmit').checked  == false) {
            alert('button not checked');
        }
    }

HTML for the same 相同的HTML

<table>
    <tr>
        <td>
            <div class="text2">
                Appointment Booking
            </div>
        </td>
        <td>
            <div class="text3">
                <span style="margin: 0 30px;">
                    <input type="radio" id="appbk1" name="appbk" value="1"><lable>1</lable></span>
                <span style="margin: 0 30px;">
                    <input type="radio" id="appbk2" name="appbk" value="2"><lable>2</lable></span>
                <span style="margin: 0 30px;">
                    <input type="radio" id="appbk3" name="appbk" value="3"><lable>3</lable></span>
                <span style="margin: 0 30px;">
                    <input type="radio" id="appbk4" name="appbk" value="4"><lable>4</lable></span>
                <span style="margin: 0 30px;">
                    <input type="radio" id="appbk5" name="appbk" value="5"><lable>5</lable></span>
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <input type="button" id="btnSubmit" value="submit" style="width: 75px;" onclick="return funcheckvalidate()" />
        </td>
    </tr>
</table>

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

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