怎么用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;
  }
}

 

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

 

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

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM