My dropdownList does not change value in the ajax call but in other parts of the js if it does

0

My problem is that I can not change the value of my dropdownlist according to the value of one of the indexes that my ajax call returns to me.

Call ajax:

    var idTets = $('select[id$=DdlTest]').val();
    $.ajax({
        type: "POST",
        url: "xxxclase/xxxmetodo",
        data: '{idTest:"' + idTets + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        success: function (result) {
            $("input[id$=textWorkName]").val(result.d[0]);
            $("textArea[id$=textWorkTopic]").val(result.d[1]);


            if (result.d[2] == "Exam") {
                $("#MainContent_dropType option[value=0]").attr('selected', 'selected');
                //$("#MainContent_dropType").val(0);
            } else {
                $("#MainContent_dropType option[value=1]").attr('selected', 'selected');
                //$("#MainContent_dropType").val(1);
            }


            if (result.d[3] == "True") {
                $("input[type=checkbox]").addClass('checked', true);
            }
            //$("#MainContent_HfOnlineTestId").val(idTets);
            //setTimeout(eventClick, 1000);
            return;
        },
        error: function (xhr, status, error) {
            alert(error);
        }
    });

I already tried it in those two ways but not an official and in fact I tried it with pure js through a for and it did not work either, the weird thing is that if I put it in a document.ready there if I change the value .

Result of the result result:

    
asked by David 22.08.2018 в 16:33
source

2 answers

0

You can select the select option through the text it contains:

$('#MainContent_dropType option:contains("texto de la opción a seleccionar")').prop('selected',true); 
    
answered by 22.08.2018 в 16:55
0

Try to select this way

Changing the attribute, from the index:

 $('#MainContent_dropType > option:eq(0)').attr('selected', true);

Changing the dropdown index

$('#MainContent_dropType').prop('selectedIndex', 3);

Selecting the option with that value.

$('#MainContent_dropType').val('valorEjemplo');
    
answered by 22.08.2018 в 16:38