js获取select的值

最近做一个手机项目时,遇到一个问题。

选分类时需要根据不同类型,改变字段。之前加的是点击事件控制显隐,火狐反应正常,但手机上没有效果。不得不抛弃这种写法,改为获取select选中option值判断,来控制字段显隐。

点击控制的代码就不写了,比较简单,贴上获取select方法代码:

<!-- 主体 -->
<div class="am-container am-margin-top reg">
      <div class="am-tabs-bd sai-noborder am-margin-top">
        <div class="am-tab-panel sai-nopadding am-active">
            <form method="post" action="{:U('Wap/Index/reg2')}" enctype="multipart/form-data"  id="form2" class="am-form am-form-horizontal">
              <div class="am-form-group">
                  <div class="reg-tab-li am-g">
                      <label class="am-u-md-3">团体名称:</label>
                      <div class="am-u-md-9">
                          <input type="text" name="o_name" class="login-text yan-tt nick" placeholder="请输入团体名称"><span></span>
                      </div>
                  </div>
                  <div class="reg-tab-li am-g">
                      <label class="am-u-md-3">类型:</label>
                      <div class="am-u-md-9">
                      <select name="o_type" id="select" onchange="getValue()">
                      <volist name="list_jigou" id="vo">
                      <option value="{$vo['d_value']}" <if condition="$i neq 4">class="dd" <else/> class="dd2"</if>>{$vo['d_title']}</option>
                      </volist>
                      </select>
                      </div>
                  </div>
                  <div class="daima2">
                  <div class="reg-tab-li am-g">
                      <label class="am-u-md-3">证件号码:</label>
                      <div class="am-u-md-9">
                      <input type="text" name="o_yingyezhizhao_no" class="login-text yan-zhzh" placeholder="请输入等级证书/营业执照等相关证件号码"><span></span>
                      </div>
                  </div>
                  <div class="reg-tab-li am-g">
                      <label class="am-u-md-3">上传扫描件:</label>
                      <div class="am-u-md-9">
                      <input type="file" name="o_yingyezhizhao_img yan-required" value=""/>
                      </div>
                  </div>
                  </div>
                  <div class="daima sai-hide">
                      
                  <div class="reg-tab-li am-g">
                      <label class="am-u-md-3">团体简介:</label>
                      <div class="am-u-md-9">
                      <textarea name="o_desc" rows="5" class="yan-desc" placeholder="请简单介绍下您所在的团体,不超过200字!"></textarea><span></span>
                      </div>
                  </div>
                  </div>
                  
                  <div class="reg-tab-li am-g">
                      <label class="am-u-md-3">创建密码:</label>
                      <div class="am-u-md-9">
                      <input type="password" name="o_password" class="login-text yan-pw" placeholder="请输入密码"><span></span>
                      </div>
                  </div>
                  <div class="reg-tab-li am-g">
                      <label class="am-u-md-3">确认密码:</label>
                      <div class="am-u-md-9">
                      <input type="password" name="o_password2" class="login-text yan-pw2" placeholder="请再次输入密码"><span></span>
                      </div>
                  </div>
                  <div class="wd-h15 wd-clear"></div>
                  <div class="reg-tab-li am-g reg-tab-li am-g2 am-margin-top">
                      <button type="button" class="l-btn btn2 am-btn am-btn-block sai-bred" onclick="sub2();">注册</button>
                  </div>
                  <div class="reg-tab-li am-g reg-tab-li am-g2">
                      <a class="wd-fs12 xieyi" target="_blank" href="{:U('Wap/Index/page',array('column_id'=>8))}">注册代表你同意<em>《爱德国际仁谷注册协议》</em></a>
                  </div>
                </div>
              </form>
        </div>
      </div>
    </div>
</div>

<script>
    ;function getValue(){
        /第一种方法
        var val= $("#select option:selected").val();
        var val= $('#select').find("option:selected").val();
/
        /第二种方法
        var obj= document.getElementById('select');
        var val = obj.selectedIndex;
/
        var obj= document.getElementById('select');
        var val = obj.options[obj.options.selectedIndex].value;
        if(val == 3){
            $('.daima2').hide();
            $('.daima').show();
        }else{
            $('.daima').hide();
            $('.daima2').show();
        }
};
</script>


当然注释的两种方法也是可行的,粗粗测试下,火狐也兼容。

是不是很好用?


js获取select的值
https://github.13sai.com/2016/08/09/63/
作者
13sai
许可协议