日期:2014-05-16  浏览次数:20368 次

问问题
本帖最后由 ouyangbo100 于 2014-03-25 15:47:41 编辑
1表示开,0表示关。用逗号连接。
编辑这个开关。
var channel = data.switchChannelStatus.length/2 + 1;
var str = "";
for(var i=1;i<=channel;i++){
str +="<div>第"+i+"路:<input type=\"radio\" value=\"1\" checked=\"checked\" id=\"rdc"+i+"1\" name=\"instruction_"+i+"\"><label for=\"rdc"+i+"1\">开启</label> <input type=\"radio\" value=\"0\" id=\"rdc"+i+"0\" name=\"instruction_"+i+"\"><label for=\"rdc"+i+"0\">关闭</label></div>"
}
}

要怎么改,可以让字符串中的数字直接在radio的checked中弄出来,而不是像我现在这样默认为1.
------解决方案--------------------
var channel = '1,0,1,1,1';
var chsp = channel.split(',');
var str = "";
    for(var i=1;i<=chsp.length;i++){

        str += chsp[i-1] == 1 ? "<div>第"+i+"路:<input type=\"radio\" checked = \"checked\" value=\"1\" id=\"rdc"+i+"1\" name=\"instruction_"+i+"\"><label for=\"rdc"+i+"1\">开启</label> <input type=\"radio\" value=\"0\" id=\"rdc"+i+"0\" name=\"instruction_"+i+"\"><label for=\"rdc"+i+"0\">关闭</label></div>" : "<div>第"+i+"路:<input type=\"radio\" value=\"1\" id=\"rdc"+i+"1\" name=\"instruction_"+i+"\"><label for=\"rdc"+i+"1\">开启</label> <input type=\"radio\" checked = \"checked\" value=\"0\" id=\"rdc"+i+"0\" name=\"instruction_"+i+"\"><label for=\"rdc"+i+"0\">关闭</label></div>";
    }
    
     document.write(str);

------解决方案--------------------
恩 把i弄成i+1
第"+(i+1)+"路