2017-06-16 67 views
0

我正在使用下面的代碼行來獲取下拉ID分配的所有值。獲取多選下拉列表中的所有值點擊按鈕

$("#assigne option:selected").each(function() { 
 
    var $this = $(this); 
 
    if ($this.length) { 
 
    var selText = $this.text(); 
 
    console.log(selText); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="row form-inline" style="padding-left:10px"> 
 
    <div class="col-md-3"> 
 
    <div class="form-group is-filled"> 
 
     <h5>Assigned </h5> 
 
     <select id="assigne" multiple="multiple" style="width:200px;"> 
 
             <option value="gnanadeep.bitra">Gnanadeep</option> 
 
             <option value="nimish.vaibhav">Nimish</option> 
 
             <option value="srividya.dommeti">Srividya</option> 
 
             <option value="shrutika.agrawal">Shrutika</option> 
 
             <option value="srinilaya.pemmaraju">Nilaya</option> 
 
             <option value="nishath">Nishath</option> 
 
             <option value="ravikiran.kothamasu">Ravi Kiran</option> 
 
             <option value="swami.sukumaran">Swami</option> 
 
             </select> 
 
    </div> 
 
    </div> 
 
</div>

但我得到一個錯誤:

Uncaught TypeError: Cannot read property 'each' of null

+0

檢查選擇的ID還檢查是否有選項 – guradio

+0

檢查。 select的id是-assigne。另外,我選擇值 –

+0

請添加'#assigne'的相關HTML。 – 31piy

回答

0

這是您的修改後的代碼:

$("#submit").on('click', function() { 
 
    var foo = []; 
 
    $('#assigne :selected').each(function(i, selected) { 
 
    foo[i] = $(selected).text(); 
 
    }); 
 
    console.log(foo); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="row form-inline" style="padding-left:10px"> 
 
    <div class="col-md-3"> 
 
    <div class="form-group is-filled"> 
 
     <h5>Assigned </h5> 
 
     <select id="assigne" multiple="multiple" style="width:200px;"> 
 
     <option value="gnanadeep.bitra">Gnanadeep</option> 
 
     <option value="nimish.vaibhav">Nimish</option> 
 
     <option value="srividya.dommeti">Srividya</option> 
 
     <option value="shrutika.agrawal">Shrutika</option> 
 
     <option value="srinilaya.pemmaraju">Nilaya</option> 
 
     <option value="nishath">Nishath</option> 
 
     <option value="ravikiran.kothamasu">Ravi Kiran</option> 
 
     <option value="swami.sukumaran">Swami</option> 
 
     </select> 
 
    </div> 
 
    </div> 
 
</div> 
 
<button id="submit">Save</button>

參考:jQuery Tip – Getting Select List Values

+0

也試過這個,它沒有幫助。謝謝。 –

+1

如何將'option:selected'改爲':selected'會有幫助嗎? '.each'仍然會出錯。 – 31piy

+0

出現錯誤「Uncaught TypeError:無法讀取屬性'null' –

相關問題