zoukankan      html  css  js  c++  java
  • 怎么用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;
      }
    }

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

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

  • 相关阅读:
    4K
    4J
    4C
    I2C总线的仲裁机制
    Linux C中strcpy , strncpy , strlcpy 的区别
    Linux下的USB总线驱动(一)
    C/C++ 语言中的表达式求值
    const变量通过指针修改问题
    关于协议栈XDATA,内存溢出的小结
    Ubuntu安装ssh,及失败解决方案
  • 原文地址:https://www.cnblogs.com/rampb/p/3479251.html
Copyright © 2011-2022 走看看