简体   繁体   中英

Set selected value in a select

I have many selectboxes in my form. Everyone have the same option, But I don't know how set the value which should be selected.

This is my code:

function addSelect(id,id_status)
    {

        $('#sel_'+id)
        .find('option')
        .remove()
        .end();


        $.ajax({
            type: "POST",
            url: "",
            data:{
                option: 'com_zamowienia',
                view: 'zamowienia_lista',
                task: 'getStatusy'
            },
            success: function(data){

                var obj = $.parseJSON(data);         
                var tabela = [];
                for (x=0; x<obj.length; x++) {
                    tabela[x] =  
                        obj[x].nazwa;    

                }
                var zmienna =0;
                $.each(tabela, function(key, value) {
                    $('#sel_'+id)
                    .append($('<option>', { value : obj[zmienna].id_status})

                    .text(tabela[zmienna])
                    .css("color", "#"+obj[zmienna].kolor)

                ); 


                    zmienna++;
                });
               $('#sel_'+id).eq(id_status).attr('selected', 'selected')

            }
        });

I use this function like this:

for(x=0;x<obj.length; x++)
                    {
                        id = obj[x].id_produkt_lista;   
                        id_status =  obj[x].id_status;

                        $("#tab").find('tbody').append(
                        "<tr id='"+id+"'><td id ='' class =''>"+obj[x].nazwa_produkt+"</td>"
                            +"<td id=''>"
                            +"<select class = 'sel_' id='sel_"+id+"' id="+id+" ></select>"   
                            +"</td>" 
                            +"</tr> ");   


                               addSelect(id,id_status);
                            //$('#sel_'+id).eq(id_status).attr('selected', 'selected');
                    }

id -> id sleectbox id_status it's value which should be selected for select which have id = "#sel_"+id

如果在这种情况下您知道“值”(id_status),则最快的方法是将值设置为给定的选择框:

$('#sel_' + id).val(id_status);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM