刷新页面后,单选按钮是否为空

时间:2011-11-24 08:33:06

标签: javascript forms radio-button

我的表格中有两个单选按钮。现在我发现在某些浏览器(firefox和谷歌浏览器)中如果刷新表单,则在刷新之前选择了一个单选按钮,然后在刷新之后仍然选中单选按钮。我想知道是否有一种方法可以使用javascript代码来表示在页面重新加载或刷新时是否已经选择了单选按钮,还是全部依赖于浏览器?

以下是javascript中的单选按钮代码:

    var btnRadioO = document.getElementsByName("weightChoice");               
                    var isbtnRadioChecked = false;

      for(i=0; i < btnRadioO.length; i++){
                        if(btnRadioO[i].checked){
                            isbtnRadioChecked = true;
                        }
                    }

                    if(!isbtnRadioChecked) {
                       errRadioMsgO.innerHTML = "Please Select whether you want to include a Total Weight for your Session or Not";
                    tblWeightO.style.display = "none";
                       isDataValid = false;
                    }else if(btnRadioO[0].checked==true && weightO.value == 0){
                         errRadioMsgO.innerHTML = "";
                         errWeightMsgO.innerHTML = "You are including Weight so it Must be More Than 0";
                         isDataValid = false;
                         }else if(btnRadioO[1].checked==true){
                    errRadioMsgO.innerHTML = "";
                     errWeightMsgO.innerHTML = "";
                    tblWeightO.style.display = "none";
                         }else{
                             errRadioMsgO.innerHTML = "";


  errWeightMsgO.innerHTML = "";
                     }

Html代码:

<table>
            <tr>
            <th>6: Provide a Total Weight for your Session</th>
            <td><input type="radio" name="weightChoice" value="yes" onClick="getWeight()"/> Yes</td>
            <td><input type="radio" name="weightChoice" value="No" onClick="getWeight()"/> No</td>
            </tr>
            </table>

1 个答案:

答案 0 :(得分:0)

可能是浏览器缓存页面。尝试在两个单选按钮上设置checked=false。如果这不起作用,请查看页面缓存选项。