JQuery and multi-select

On my page, I need to select the parameter values ​​loaded by the $ .post function. I have a button for “moving” data from one selection to another. The example works on a new page, but not on my page. Selected items do not move to another selection after clicking.

The question is, is there any other way to do this, or how can I fix my page to make it work?

Code example:

<div style="float:left; border:1px solid red;">
    <div style="background-color:red; color:white;" align="center">INFORMATIVE</div>
    <div id="prop_info" style="float:left;">
        <div style="border:1px solid red;">PROPOSTI</div>
        <div style="float:left"><select multiple id="sel_info_pro" name="sel_info_pro" class="sel"><option value="b">a</option><option>b</option></select></div>
        <div style="float:left;"><a href="#" id="add_info"> >> </a><br><a href="#" id="remove_info"> << </a></div>
    </div>  
    <div id="conf_info" style="float:left;">
        <div style="border:1px solid red;">CONFIGURATI</div>
        <select multiple id="sel_info_conf" name="sel_info_conf" class="sel"></select>
    </div>
</div>
$(document).ready(function(){
    $('#add_info').click(function() {
        return !$('#sel_info_pro option:selected').remove().appendTo('#sel_info_conf');
    });
    $('#remove_info').click(function() {
        return !$('#sel_info_conf option:selected').remove().appendTo('#sel_info_pro');
    });
});
+3
source share
2 answers

I found my working solution:

$(document).ready(function(){
$('#add_info').click(function() {
    var foop = []; 
    $('#sel_info_pro option:selected').each(function(z, selected){ 
        foop[z] = $(selected).val(); 
        return !$('#sel_info_pro option:selected').val(foop[z]).remove().appendTo('#sel_info_conf'); 
    });
}); 
$('#remove_info').click(function() { 
    var foo = []; 
    $('#sel_info_conf option:selected').each(function(i, selected){ 
        foo[i] = $(selected).val(); 
        return !$('#sel_info_conf option:selected').val(foo[i]).remove().appendTo('#sel_info_pro'); 
    });
});

});

Thank you all for your help!

0
source

A little guide that might help. If you have done the following:

alert($('#sel_info_pro option:selected').length);

Then you get 0, no matter how much you choose

val() jQuery:

alert($('#sel_info_pro option').val().length); //gives you the count of options selected
alert($('#sel_info_pro option').val()[0]); //gives you the value of the first option selected

, . , , , .

: http://marcgrabanski.com/articles/jquery-select-list-values

+1

Source: https://habr.com/ru/post/1759842/


All Articles