var radioLength = radi">

JavaScript判断Form中的Radio是否有未选中的

80酷酷网    80kuku.com

  javascript|选中

function getCheckedValue(radioObj) ...{
    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) ...{
        if(radioObj[i].checked) ...{
            return radioObj[i].value;
        }
    }
    return "";
}
function check()
...{

for(i = 0; i < document.submitform.all.length; i++)...{
  if(document.submitform.all(i).tagName == "INPUT")...{
    var oitem = document.submitform.all.item(i);
    var sname = oitem.name;
    if(oitem.type.toLowerCase()=="radio")...{
      if(getCheckedValue(document.getElementsByName(oitem.name)) == "")...{
        oitem.focus();
        window.scrollBy(0, -30);
        alert("请认真填写所有题目。");
        return (false);
      }
    }
  }
}

document.submitform.submit();

}

getCheckedValue 获取单选框的值,

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
 if(!radioObj)
  return "";
 var radioLength = radioObj.length;
 if(radioLength == undefined)
  if(radioObj.checked)
   return radioObj.value;
  else
   return "";
 for(var i = 0; i < radioLength; i++) {
  if(radioObj[i].checked) {
   return radioObj[i].value;
  }
 }
 return "";
}
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
 if(!radioObj)
  return;
 var radioLength = radioObj.length;
 if(radioLength == undefined) {
  radioObj.checked = (radioObj.value == newValue.toString());
  return;
 }
 for(var i = 0; i < radioLength; i++) {
  radioObj[i].checked = false;
  if(radioObj[i].value == newValue.toString()) {
   radioObj[i].checked = true;
  }
 }
}

check函数,判断所有单选框是否有未选择的,如果有显示提示.



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: