用js代码改变单选框选中状态的简单实例


今天突然有一个需求要用到,使用js代码改变单选框的选中状态。当时想也不想直接

复制代码 代码如下:

function doGender(gender) {
  if (gender == "男") {
    gel("radionan").style.checked = "checked";
  } else {
    gel("radionv").style.checked = "checked";
  }
}

function gel(id) {
  return document.getElementById(id);
}


一执行,没反应......

因为我们在radio标签中设置选中是checked="checked";所以下意识给gel("radionv").style.checked赋值为"checked",然后上网一查

原来在js代码中要选中该单选框要给checked赋值为true。

继续改为:

复制代码 代码如下:

function doGender(gender) {
  if (gender == "男") {
    gel("radionan").style.checked = true;
  } else {
    gel("radionv").style.checked = true;
  }
}

一执行,还是没反应,有点蒙了...哪里出错呢????

难道不是style属性么????

直接gel("radionan")点一下,可以看到checked属性。

那就没错了!!!!!

复制代码 代码如下:

function doGender(gender) {
  if (gender == "男") {
    gel("radionan").checked = true;
  } else {
    gel("radionv").checked = true;
  }
}

一执行,果然如此。。。。。。。。。。。。。。

一切到此结束!!!!!!!!!!!!


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3