Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I am binding values(options) to droopdown dynamically through webservice like this
JavaScript
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "MaterialRequestForm.aspx/BindBatches",
    data: "{'ItemCode':'" + ui.item.val + "'}",
    dataType: "json",
    success: function (data) {
        var seldata = [];
        $(id).empty().append("Select");
        $.each(data.d, function (key, value) {
          
$(id).append($("").val(value.BatchNum).html(value.Batch));
          
        });
    },
    error: function (result) {
        alert("Error");
    }
});

Now what I need is
If no option append to dropdown then append static text "Not available" to dropdown.

how to append static value dropdown on value empty???

What I have tried:

JavaScript
if($(id).find('option').length > 0)
{
$(id).append($("").val(value.BatchNum).html(value.Batch));
}
else
{
$(id).append("Not Available");
}
Posted
Updated 8-Aug-17 19:30pm
v2

1 solution

try this

success: function (data) {

               var options = ["<option value=''>Select</option>"];
               if (data.d && data.d.length > 0) {
                   $.each(data.d, function (key, value) {
                       options.push("<option value='" + value.BatchNum + "'>" + value.Batch + "</option>");
                   });
               }
               else
                   options = ["<option value=''>Not Available</option>"];
               var html = options.join('');
               document.getElementById(id).innerHTML = html;


           }
 
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