doushi9474 2013-09-06 06:54
浏览 27
已采纳

使用JavaScript验证单选按钮[关闭]

i'm trying to follow this tutorial guide on this website http://homepage.ntlworld.com/kayseycarvey/jss3p11.html?

However, i met some difficultly when doing so. Even though i did the exact same thing. Please guide me if you know so. Here are what i did:

<html>
    <script>
        function GetSelectedItem() {

            chosen = ""
            len = document.f1.r1.length

            for (i = 0; i <len; i++) {
                if (document.f1.r1[i].checked) {
                    chosen = document.f1.r1[i].value
                }
            }

            if (chosen == "") {
                alert("No Location Chosen")
            }
            else {
                alert(chosen) 
            }
        }
    </script>

    <body>
        <form name="f1">
        <Input type = radio Name = r1 Value = "NE">North East
        <Input type = radio Name = r1 Value = "NW">North West
        <Input type = radio Name = r1 Value = "SE">South East
        <Input type = radio Name = r1 Value = "SW">South West
        <Input type = radio Name = r1 Value = "midlands">Midlands
        </form>


    </body></script>
</html>

On a side note, do i have to do anything to the form in order to trigger the function GetSelectedItem ? Thanks in advance !

  • 写回答

4条回答 默认 最新

  • drz5553 2013-09-06 06:58
    关注

    add GetSelectedItem ()function on onchange event of your radio button

         <input type="radio" onclick="GetSelectedItem()" value="NE" name="r1">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码