Click here to Skip to main content
16,011,868 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
Dear Friends,

I am getting the data by calling the web method via Jquery and need to bind the data to dropdownlist box.

But I cant bind the same. The code I have used is below.
JavaScript
var dropdown = $("#ddl_req_by");

   var json = msg.d;
   $.each($.parseJSON(json), function () {
   alert(this.REQ_BY);
  $("#ddl_req_by").append($("<option></option>").val(this.REQ_BY).html(this.REQ_BY));
                                    
});

Please can one guide me on this.
Posted
Updated 26-Nov-13 17:43pm
v3
Comments
Sergey Alexandrovich Kryukov 26-Nov-13 11:37am    
Could you show relevant part of HTML? At least the elements with id="dropdownbox1" and id="ddl_req_by". (Do you even have elements with those two attributes, exactly?)
—SA

1 solution

You can follow the below mentioned example and adjust it according to your situation.

View

XML
<select id="purchasedPackages" name="purchasedPackages">
                    </select>



JavaScript

<script type="text/javascript">
  Sys.require('jQuery', function () {
     
                    $.ajax({
                        url: "/Portal/GetPurchasedPackages",
                        dataType: 'json',
                        data: { },
                        success: function (data) {
                            $("#purchasedPackages").fillSelect(data);
                        }
                    });

                    $.fn.fillSelect = function (data) {
                        return this.clearSelect().each(function () {
                            if (this.tagName == 'SELECT') {
                                var dropdownList = this;
                                $.each(data, function (index, optionData) {
                                    var option = new Option(optionData.Text, optionData.Value);
                                    if ($.browser.msie) {
                                        dropdownList.add(option);
                                    } else {
                                        dropdownList.add(option, null);
                                    }
                                });
                            }
                        });
                    };

                    $.fn.clearSelect = function () {
                        return this.each(function () {
                            if (this.tagName == 'SELECT')
                                this.options.length = 0;
                        });
                    };
</script>
});


I hope this will help to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900