Fill a jquery combobox

Asked

Viewed 482 times

1

See is not popular a combo, I do. The combo is populated, then I need to take the id and show in the combo the text referring to that ID. The ID I have in my table, all that remains is to show the text in the combo. My jquery is like this:

function CarregaDados(ajaxParameter) {
    $.ajax({

        url: '/CadastroAcesso/CarregaDadosPagina',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
        data: JSON.stringify({ _nivel: ajaxParameter }),
        success: function (data) {

            $.each(data, function (index, itemData) {
                $('#txtNome').val(itemData.NM_Usuario);
                $('#txtUsuarioRede').val(itemData.Usuario1);
                $('#txtEmail').val(itemData.Email);
                //$('#cbxNivelAcesso').val();
            });
        },
        error: function (error) {

        }
    })
}

cbxNivelAccess is my combo. It is commented, because I tested it in various ways and nothing.

1 answer

1

I’m not sure what you want. But I’ll show you two options, either by taking the value of the selected text from the combo, or by passing the value of the option to retrieve the text.

$("#cbxNivelAcesso").children("option").filter(":selected").text();

or passing the value

$("#cbxNivelAcesso option[value='2']").text()
  • Let me explain further. In my combo there are three records, with ID’s: 1, 2 and 3. When the ID = 3, the text should be shown is: ACTION. If the ID = 2 the text to be shown is: Query and if it is ID = 1 the text is: ADMINISTRATOR. This is what I cannot.

  • This text should be shown where ? Copy your combo and glue it here please.

  • In the combobox. Like this. There is a table and when the user gives a double click, there are four fields that will be filled to be edited and one of these fields is the combobox

  • I think I get it, the person selects a value and you want it dynamically loaded in the combo ?

  • 1

    If so, try to leave the combo selected as $("#cbxNivelAccess option[value='3']"). attr('Selected','Selected'); Ai no value you can swap the constant for a variable.

  • Vinicius, that solves, now I can’t carry the Idnivel, because he’s a Foreign Key. But I will close this topic and try to resolve the other issue. If I can’t, I open another post.

  • Answer Vinicius for me to close with your answer.

  • This solved: $('#cbxNivelAccess option[value=' + itemData.Idnivel_access + ']'). attr('Selected', 'Selected');

  • I’m sorry, buddy, I was in college. Too cool, if there’s anything else I can do to help you, you can be in charge.. But if not, please finish the post with a joinha. Hug ;)

  • How do I put a joinha in my reply or comment to close the post?

  • In my post you click the arrow up and just below it you will have an option to accept. : D

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.