如下列代码中,有早餐,午餐2组radio,每组都有4个radio可以选,我想从数据库中读出breakfast或者lunch的值,然后使用JS根据不同的值选择不同的radio,请问代码应该怎样?
早餐
      <input name="breakfast" type="radio" id="breakfast4" value="无" />
      <label for="breakfast4">无</label>
      <input name="breakfast" type="radio" id="breakfast1" value="1" />
      <label for="breakfast1">1份</label>
      <input type="radio" name="breakfast" id="breakfast2" value="2" />
      <label for="breakfast2">2份</label>
      <input name="breakfast" type="radio" id="breakfast3" value="3" />
      <label for="breakfast3">3份</label>
午餐
      <input name="lunch" type="radio" id="lunch4" value="无" />
      <label for="lunch4">无</label>
      <input name="lunch" type="radio" id="lunch1" value="1" />
      <label for="lunch1">1份</label>
      <input type="radio" name="lunch" id="lunch2" value="2" />
      <label for="lunch2">2份</label>
      <input name="lunch" type="radio" id="lunch3" value="3" />
      <label for="lunch3">3份</label>

解决方案 »

  1.   


    <script>
    window.onload=function(){
      var bf_val = "1";  // 服务器端赋值 var bf_val = "<%=rs("breakfast")%>";
      var lc_val = "2";  // 服务器端赋值
      var bf = document.forms[0].breakfast;
      var lc = document.forms[0].lunch;
      for(var i=0;i<bf.length;i++){
        if(bf[i].value==bf_val) {bf[i].checked=true;break;}
      }
      for(var i=0;i<lc.length;i++){
        if(lc[i].value==lc_val) {lc[i].checked=true;break;}
      }
    }
    </script>
    <form>
    早餐
          <input name="breakfast" type="radio" id="breakfast4" value="无" />
          <label for="breakfast4">无</label>
          <input name="breakfast" type="radio" id="breakfast1" value="1" />
          <label for="breakfast1">1份</label>
          <input type="radio" name="breakfast" id="breakfast2" value="2" />
          <label for="breakfast2">2份</label>
          <input name="breakfast" type="radio" id="breakfast3" value="3" />
          <label for="breakfast3">3份</label>
    午餐
          <input name="lunch" type="radio" id="lunch4" value="无" />
          <label for="lunch4">无</label>
          <input name="lunch" type="radio" id="lunch1" value="1" />
          <label for="lunch1">1份</label>
          <input type="radio" name="lunch" id="lunch2" value="2" />
          <label for="lunch2">2份</label>
          <input name="lunch" type="radio" id="lunch3" value="3" />
          <label for="lunch3">3份</label>
    </form>
      

  2.   


    var breakfasts = document.getElementsByName("breakfast");
    for(i=0;i<breakfasts.length;i++){
        if(breakfasts[i]=="某值"){
            breakfasts[i].checked;
        }
    }???
      

  3.   

    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>test</title>
    </head><body>
    早餐
          <input name="breakfast" type="radio" id="breakfast4" value="无" />
          <label for="breakfast4">无</label>
          <input name="breakfast" type="radio" id="breakfast1" value="1" />
          <label for="breakfast1">1份</label>
          <input type="radio" name="breakfast" id="breakfast2" value="2" />
          <label for="breakfast2">2份</label>
          <input name="breakfast" type="radio" id="breakfast3" value="3" />
          <label for="breakfast3">3份</label>
    午餐
          <input name="lunch" type="radio" id="lunch4" value="无" />
          <label for="lunch4">无</label>
          <input name="lunch" type="radio" id="lunch1" value="1" />
          <label for="lunch1">1份</label>
          <input type="radio" name="lunch" id="lunch2" value="2" />
          <label for="lunch2">2份</label>
          <input name="lunch" type="radio" id="lunch3" value="3" />
          <label for="lunch3">3份</label>
    <script>
    function setRadio(radioName,v){
    var radioes = document.getElementsByName(radioName);
    for(var i=0;i<radioes.length;i++){
    if(radioes[i].value==v){
    radioes[i].checked = true;
    }
    }
    }
    setRadio("breakfast",2);//参数1:radio的name,参数2:你取到的值
    setRadio("lunch",3);
    </script>
    </body></html>
      

  4.   

    呵呵,没什么的,分小事,重要的是在CSDN共同学习、共同进步的过程