选择单选按钮 [英] Selection radio button

查看:188
本文介绍了选择单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持在哪里从这里走了一下。我想我的单选按钮(县)返回选定按钮的值。但现在,它只有返回的第一个项目的价值。我怎样才能使它遍历所有的值,这里是我的code。

 <!DOCTYPE HTML>
< HTML和GT;
<身体GT;
    <?PHP的包括db_connector.php';
        $结果= mysqli_query($ CON,选择的名字从os_scope);
        而($行= mysqli_fetch_array($结果)){
            $行[名称];
            回声<输入类型='无线电'名称='操作系统'值=$行['名字'。ID ='myRadio'>中;
        }
    ?>    < P>点击了试试吧按钮,显示的单选按钮的值< / P>
    <按钮的onclick =myFunction的()>试用< /按钮>
    < p n =演示>< / P>    <脚本>
        功能myFunction的(){
            。VAR X =的document.getElementById(myRadio)值;
            的document.getElementById(演示)的innerHTML = X。
        }
    < / SCRIPT>
< /身体GT;
< / HTML>


解决方案

 <脚本>
    功能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>

这篇关于选择单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆