2013-02-20 8 views
7

Mam problem w Get All wybrana opcja w wielu wybierzJak zdobyć wszystko wybrana opcja forma tekst wielo wybrać przy użyciu JavaScript

<select multiple="" title="" class="" id="fm_delivery_or_collection" name="fm_fields[fm_delivery_or_collection][]"> 
    <option value="90">Delivery or Collection1</option> 
    <option value="91">Delivery or Collection2</option> 
    <option value="92">Delivery or Collection3</option> 
</select> 

Bellow jest kod i jego powrotu do mnie tylko pierwsza wybrana opcja

var select = form.find('select') 

for (var i = 0; i < select.length; i++) 
     { 
      var s_id = jQuery(select[i]).attr('id'); 
      var str="",i; 

      var e = document.getElementById(s_id); 
      var strUser = e.options[e.selectedIndex].text; 

      var name = jQuery(select[i]).attr('name') 
      var str1 = jQuery(select[i]).attr('id').replace("fm_"," ") 
      requestString += "<b>"+str1.replace(/_/g," ")+"</b>" + ':' +strUser+"<br>"; 
     } 

Więc proszę zasugeruj mi, jak mogę uzyskać wszystkie wybrane teksty opcji i gdzie popełniam błąd?

+0

Wartość 'wybrać multiple' (lub wybierz' [select.selectedIndex] ') jest tablicą ... – Teemu

+0

Odpowiedzi są zbyt skomplikowane. Możesz uzyskać wszystkie zaznaczone opcje za pomocą .map(). '$ (" select: selected "). map (function (i, element) {return jQuery (element) .text();}). get();' –

Odpowiedz

23

Twój komentarz please suggest me how can i get all selected option text, So you can try this:

$("#fm_delivery_or_collection option:selected").each(function() { 
    var $this = $(this); 
    if ($this.length) { 
    var selText = $this.text(); 
    console.log(selText); 
    } 
}); 
2

Spróbuj z tym

$("#fm_delivery_or_collection option").each(function(){ 
    if($(this).attr('selected') == 'selected') 
    { 
     var name = $("#fm_delivery_or_collection").attr('name') 
     var str1 = $("#fm_delivery_or_collection").attr('id').replace("fm_"," ") 
     requestString += "<b>"+str1.replace(/_/g," ")+"</b>" + ':' +strUser+"<br>"; 
    } 
}) 
+0

Czy to działa? – Gautam3164

+0

Witaj .......... @ user123 czy działa ... musisz odpowiedzieć – Gautam3164

+0

Okay Gautam, dlaczego to zadziała? – Jai

11
// Return an array of the selected opion values 
// select is an HTML select element 
function GetSelectValues(select) { 
    var result = []; 
    var options = select && select.options; 
    var opt; 

    for (var i=0, iLen=options.length; i<iLen; i++) { 
    opt = options[i]; 

    if (opt.selected) { 
     result.push(opt.value || opt.text); 
    } 
    } 
    return result; 
} 

przekazać tę funkcję Twój wybierz pole jako jak ..

var selectBox = document.getElementsById('fm_delivery_or_collection'); 
alert(GetSelectValues(selectBox)); 

zwróci tablicę wybranych wartości

Dzięki użyciu jQuery

$('select#fm_delivery_or_collection').val() 

Funkcja val wywołana z select zwróci tablicę, jeśli jest to wielokrotność.

0
$("#id :selected").each(function (i,sel) { 
    alert($(sel).text()); 
}); 

nadzieję, że pomoże ..

0

try this link:

$("#selection option:selected").each(function() { 
    var $this = $(this); 
    if ($this.length) { 
    var selText = $this.text(); 
    console.log(selText); 
    $('#selected').append(selText+', '); 
    } 
}); 
Powiązane problemy