4

I have json data as below

{ "aaData": [["2","MAC"],["3","Apple"],["5","Windows"],["5","Unix"],["6","Linux"]]}

And I try with below jquery code but data not add with select tag

<script type="text/javascript">
    $(document).ready(function() {
    $("#mc_category").change(function() {
            $.getJSON("/admin/getSubCategory.php", null, function(data) {
            $("#subcategory").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);
               }
           });
     }
    });
}

});
</script>
2
  • updated subcategory is select tag Commented Apr 20, 2011 at 18:50
  • you probably just needed $("#subcategory").fillSelect(data.aaData); Commented Apr 20, 2011 at 19:05

2 Answers 2

5

You can just loop through the aaData array, and use the jQuery appendTo method to add each item to your select box.

I'm not sure what the fillSelect method does, but here's an implementation that you can replace it with:

var sel = $('#subcategory');
for (var i = 0; i < data.aaData.length; i++)
{
    var e = data.aaData[i];
    $('<option>').text(e[1]).val(e[0]).appendTo(sel);
}
Sign up to request clarification or add additional context in comments.

Comments

2
$("#subcategory").append(
     $.map(data.aaData,function(index,value){
          return $("<option/>").attr("value",value[0]).text(value[1]);
     }).get();
 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.