JS 는 라디오 와 checkbox 가 선택 되 었 는 지 검증 합 니 다.

2311 단어 JavaScripthtml

<html>
<head>
    <title></title>
    <script src="JavaScript.js"></script>
    
</head>


<body>
    <form id="form" name="form" method="post" action="http://js.alixixi.com" onsubmit="return CheckForm()">
        <input type="checkbox" name="love" >
        <input type="checkbox" name="love" >
        <input type="checkbox" name="love" >
        <input type="checkbox" name="love" >
        


        <input type="radio" value="  " name="miaoshu" >
        <input type="radio" value="  " name="miaoshu" >
        <input type="radio" value="  " name="miaoshu" >
        <input id="txtbox" name="MyTextfield" type="text">
        
        <input type="submit" value="  " >
    </form>


</body>
</html>



function CheckForm() {
    var flag = false;
    var allRadio = document.form.elements["miaoshu"];
    var txtBox = document.getElementById("txtBox").value;
    for (var i = 0; i < allRadio.length; i++) {
        if (allRadio[i].checked) {
            if (allRadio[allRadio.length - 1].checked) {
                if (txtBox == "") {
                    flag = false;
                }
                else {
                    flag = true;
                }

            }
        }
    }
    if (!flag) {
        alert("       ");
    }

    var cbxs = document.getElementsByName('love');
    var s = 0;
    for (var i = 0; i < cbxs.length; i++) {
        if (cbxs[i].checked)
        { s += 1; }
    }
    if (s < 1) {
        alert("       ");
        flag = false;
    }
    return flag;
}


좋은 웹페이지 즐겨찾기