如何获取div下的select标签的值

时间:2014-10-24 13:37:53

标签: jquery

我有一个HTMl,如下所示

<div class="cascade" id="T1">
   <div class="row-fluid">
      <div class="col9 pull-left">
         <select class="m-wrap" tabindex="1" style="width:100%;">
            <option class="placeholder" selected="" disabled="">Select T1</option>
            <option value="Ice Creams">Ice Creams</option>
            <option value="Popcorn">Popcorn</option>
         </select>
      </div>
      <div class="col1 pull-left"><a href="#" class="btn mini green" style="margin-left:10px;"><i class="icon-plus"></i></a><a href="#" class="btn mini black"><i class="icon-trash"></i></a></div>
      <div class="row-fluid">
         <div class="col9 pull-left"></div>
      </div>
   </div>
</div>

我试过这种方式,但我得到了无效

var T1 = $('#T1').find(".m-wrap").val();

alert(T1);

任何人都可以帮助我

3 个答案:

答案 0 :(得分:2)

您获得null因为当前selected设置为实际 <option>的{​​{1}}。

使用disabled meta

:selected

Fiddle Example

答案 1 :(得分:0)

您必须删除已禁用的属性(已禁用=&#34;&#34;)及其工作

<option class="placeholder" selected="">Select T1</option>

或者,如果您想保留已禁用的属性,则必须像这样调用所选选项

var T1 = $('#T1').find(".m-wrap option:selected").val();

答案 2 :(得分:0)

<html>
   <head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
     <script type="text/javascript">
      function getSelectedValue(){
         var T1 = $('#T1').find(".m-wrap").val();
         alert(T1);
      }
   </script>
   </head>
   <body>
      <div class="cascade" id="T1">
         <div class="row-fluid">
            <div class="col9 pull-left">
               <select class="m-wrap" tabindex="1" style="width:100%;" onclick="getSelectedValue()">
                  <option class="placeholder" selected="" disabled="">Select T1</option>
                  <option value="Ice Creams">Ice Creams</option>
                  <option value="Popcorn">Popcorn</option>
               </select>
            </div>
            <div class="col1 pull-left"><a href="#" class="btn mini green" style="margin-left:10px;"><i class="icon-plus"></i></a><a href="#" class="btn mini black"><i class="icon-trash"></i></a></div>
            <div class="row-fluid">
               <div class="col9 pull-left"></div>
            </div>
         </div>
      </div>
   </body>
<html>

请试试这个。感谢。