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

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

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

  • 相关阅读:
    C# 判断系统空闲(键盘、鼠标不操作一段时间)
    c#做动态(gif)中文验证码
    vue 移动端公众号采坑经验
    git常用命令汇总
    关于面试的相关问题
    jQuery请求后台接口
    谈谈前端行业一两点
    JQuery获取Dom元素的方法
    Vue--props
    函数的上下文就是函数里面的this是谁
  • 原文地址:https://www.cnblogs.com/rampb/p/3479251.html
Copyright © 2011-2022 走看看