1

In the below code i have dynamic dropdown and i want a value to be selected.But when i pass a value to be selected.No values are getting binded.Ples help me to do this.

 if (type == "DropDownList") {
            newdiv.innerHTML += "<select class=\"form-control\" data-error=\"Please Provide " + DisplayName + "\"  name=\"" + Name + "\" id=\"" + Name + "\"/>";
               $('#divComplete').append(newdiv);
            Load(id, Name, FieldValue);
        }


 function Load(sFieldID, sControleID, FieldValue) {

        alert(FieldValue);        if (sFieldID == '')
            return;

        var dataString = JSON.stringify({
            strProjectID: iProjectID

        });

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Sample.aspx/test",
            dataType: "json",
            async: false,
            data: dataString,
            success: function (data) {
                var locations = JSON.parse(data.d);
                $('#' + sControleID).html("");
                $.each(locations, function (i, item) {
                    alert(item);

                    $('#' + sControleID).append($('<option selected=\"true\"  value=\"' + FieldValue + '\">', {
                        value: item.EnumText,
                        text: item.EnumText
                    }));
                });

            },
            error: function (result) {
            }
        });
    }
Dotnet
  • 109
  • 12
  • 2
    What is the value of `id` (passed to `Load()`)? Is it set? Are your `alerts()` firing with miltiple `item`s, as expected? What does `data.d` contain? – Paul Roub Nov 02 '15 at 20:51
  • Possible duplicate of [Set selected option of select box](http://stackoverflow.com/questions/4680075/set-selected-option-of-select-box) – IROEGBU Nov 03 '15 at 01:31

1 Answers1

0

you can use jQuery's .val() method on the tag:

$('select').val('c');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select>
  <option>a</option>
  <option>b</option>
  <option>c</option>
  <option>d</option>
</select>
Stefan
  • 3,962
  • 4
  • 34
  • 39
  • did i misunderstand your question? You want to dynamically select the value of a `select` tag, that's what the code above does. If you click "run code snippet" you'll get a select with the value of `c` which was the 3rd option. removing the jquery snippet would render a `select` with the default value of `a` – Stefan Nov 03 '15 at 18:00