且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

选择单选按钮

更新时间:2022-10-16 21:41:39

 <脚本>
    功能myFunction的(){
        变种X = document.getElementsByName('口');
        VAR rate_value;
        对于(VAR I = 0; I< x.length;我++){
            如果(X [I] .checked){
                rate_value = X [I] .value的;
                打破;
            }
        }
        的document.getElementById(演示)的innerHTML = rate_value。    }
< / SCRIPT>

Am a bit stuck on where to go from here. I would like my radio button(s) to return the value of the selected button. But right now, its only return the value of the first item. How can I make it loop through all the values, here is my code.

<!DOCTYPE html>
<html>
<body>
    <?php include 'db_connector.php';
        $result = mysqli_query($con,"SELECT name FROM os_scope");
        while($row = mysqli_fetch_array($result)) {
            $row['name'];
            echo "<input type='radio' name='os' value=".$row['name']." id='myRadio'>";
        }
    ?>

    <p>Click the "Try it" button to display the value of the radio button.</p>
    <button onclick="myFunction()">Try it</button>
    <p id="demo"></p>

    <script>
        function myFunction() {
            var x = document.getElementById("myRadio").value;
            document.getElementById("demo").innerHTML = x;
        }
    </script>
</body>
</html>

<script>
    function myFunction() {
        var x = document.getElementsByName('os');
        var rate_value;
        for(var i = 0; i < x.length; i++){
            if(x[i].checked){
                rate_value = x[i].value;
                break;
            }
        }
        document.getElementById("demo").innerHTML = rate_value;

    }
</script>