为什么使用switch语句不行?

来源:9-4 区别getElementByID,getElementsByName,getElementsByTagName

qq_潘虫虫虫虫虫虫_03313943

2016-05-22 15:18

 var ho=document.getElementsByName("hobby"); switch(j){ case 1: ho[0].checked=true; break; ase 2: ho[1].checked=true; break; case 3: ho[2].checked=true; break; case 4: ho[3].checked=true; break; case 5: ho[4].checked=true; break; case 6: ho[5].checked=true; break; default: }

写回答 关注

2回答

  • inro
    2016-06-15 16:22:34
                switch(j)
                {
                    case "1":
                        var hobby = document.getElementById("hobby1");
                        if (false == hobby.checked)
                        {
                            hobby.checked = true;
                        }
                        break;
                     case "2":
                        var hobby = document.getElementById("hobby2");
                        if (false == hobby.checked)
                        {
                            hobby.checked = true;
                        }
                        break;
                    case "3":
                        var hobby = document.getElementById("hobby3");
                        if (false == hobby.checked)
                        {
                            hobby.checked = true;
                        }
                        break;
                    case "4":
                        var hobby = document.getElementById("hobby4");
                        if (false == hobby.checked)
                        {
                            hobby.checked = true;
                        }
                        break;
                    case "5":
                        var hobby = document.getElementById("hobby5");
                        if (false == hobby.checked)
                        {
                            hobby.checked = true;
                        }
                        break;
                    case "6":
                        var hobby = document.getElementById("hobby6");
                        if (false == hobby.checked)
                        {
                            hobby.checked = true;
                        }
                        break;
                    default:
                        var j = document.getElementById("wb");
                        j.value="";
                        j.placeholder="请输入1-6之间的数字";
                        alert("请输入1-6之间的数字");
                        
                        break;
                }

    当然可以了。

  • 幽火
    2016-05-24 00:14:11

    ase 2 这里少了一个子母C

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468061 学习 · 21891 问题

查看课程

相似问题