欢迎来到3672js教程,我们关注js教程、js框架、js代码特效等。

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

3672Js.Com2019-03-09 09:17 来源:未知 阅读:1870 关注度3

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

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

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

本站文章为3672js教程网友分享投稿,版权归原作者,欢迎任何形式的转载,但请务必注明出处。同时文章内容如有侵犯了您的权益,请联系我们处理。
评论已被关闭
{dede:include filename="foot.htm"/}