简体   繁体   中英

How to check to make sure a radio button is selected

I have a form that I need to figure out the code on how to make sure a radio button is selected

Here is the form

<body>
    <div id="content">

        <p>Enter the values below and click "Calculate".</p>

        <label for="length">Length:</label>
        <input type="text" id="length" /><br />

        <label for="width">Width:</label>
        <input type="text" id="width" /><br />

        <label for="answer">Answer:</label>
        <input type="text" id="Answer" disabled="disabled" /><br />

        <input type="radio" name="Area" value="Area" check="checked"/>Area<br />
        <input type="radio" name="Parimeter" value="Parimeter"  />Parimeter<br />

         <label>&nbsp;</label>
        <input type="button" id="calculate" value="Calculate" /><br />

    </div>
</body>
</html>

here is the code

var $ = function (id) {
    return document.getElementById(id);
}

var calculate_click = function () {
    var length = parseFloat( $("length").value );
    var width = parseFloat( $("width").value );

    // $("area").value = "";

    if (isNaN(length) || length <= 0) {
        alert("Length must be a valid number\nand greater than zero.");
    } else if(isNaN(width) || width <= 0) {
        alert("Width must be a valid number\nand greater than zero.");
       }
    } else {
        var area = width * length;
        var perimeter = 2 * width + 2 * length;

        $("area").value = area.toFixed(2);
        $("perimeter").value = perimeter.toFixed(2);
    } 
}

window.onload = function () {
    $("calculate").onclick = calculate_click;
    $("length").focus();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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