为了账号安全,请及时绑定邮箱和手机立即绑定

第三个问题这样可以吗?符合开发规范吗?

第三个问题这样可以吗?符合开发规范吗?

        function checkone(){
            var j=document.getElementById("wb").value;
            var x=document.getElementsByName("hobby");
            for(var i=1;i<x.length;i++){
            if(j.indexOf(i)>-1){x[i-1].checked=true;}}


正在回答

5 回答

    var j=document.getElementById("wb").value;        

         // 任务3

            var idStr="hobby"+j;

            document.getElementById(idStr).checked=true;

非常简单啊,没有那么复杂,拿去吧,这是我写的

0 回复 有任何疑惑可以回复我~

上面returnDocument(); 是进行了封装了的,代码如下:

 function returnDocument(theId){
          return document.getElementById(theId);
        }


0 回复 有任何疑惑可以回复我~

不可以的,这样无法实现功能。

indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。这里使用不符合。

参考代码:

function checkone(){
            var getValue= document.getElementById("wb").value;
        
         // 任务3
            switch(getValue){
              case '1': returnDocument("hobby1").checked = true;
                      break;
              case '2': returnDocument("hobby2").checked = true;
                      break;
              case '3': returnDocument("hobby3").checked = true;
                      break;
              case '4': returnDocument("hobby4").checked = true;
                      break;
              case '5': returnDocument("hobby5").checked = true;
                      break;
              case '6': returnDocument("hobby6").checked = true;
                      break;
              default:
                alert("输入超出范围!");
                break;               
            }
        }


0 回复 有任何疑惑可以回复我~

不符合,变量名称必须有一定的含义,增加代码的可阅读性

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

第三个问题这样可以吗?符合开发规范吗?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信