Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Fred Travels</title>
    <script src="jquery.min.js"></script>
    <script>
        $(document).ready(function () { //page load
            $.ajax({
                type: "GET",
                url: "http://localhost:2160/CustomerService.svc/CustomerTypeSelect",
                contentType: "application/json; charset=utf-8",
                data: {},
                dataType: "json",
                processData: true,
                success: function (data, status, jqXHR, result) {
                    if (data.length > 0) {
                        var $el = $("#ddlCustType");
                        $el.empty(); // remove old options
                        $el.append($("<option></option>").attr("value", 0).text('Please Select'));
                        for (var i = 0; i < data.length; i++) {
                            $el.append($("<option></option>").attr("value", data[i].CusTypeID).text(data[i].CustTypeName));
                        }
                    }
                }
            });
            CustomerSelectviaID(1);
        });

        function CustomerSelectviaID(CustID) {
            var Customer = {
                rowno: 1,
                TotalRecords: 5,
                CustomerID: CustID
            };
            $.ajax({
                type: "POST",
                url: "http://localhost:2160/CustomerService.svc/CustomerSelect",
                data: JSON.stringify(Customer),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                processData: true,
                success: function (data, status, jqXHR, result) {
                    if (data.length > 0) {  
alert("test");                     
                        $('#ddlCustType').val(data[0].Type);                       
                        return false;
                    }
                }
            });
            $('#hdnCustID').val(CustID);
        }
    </script>
</head>
<body>
    <div>
        <select id="ddlCustType" class="form-control">
            <option value="0">Please Select</option>
            <option value="1">Corporate</option>
            <option value="2">Tour Operator</option>
            <option value="3">Direct Customer</option>
            <option value="4">out source</option>
        </select>
    </div>
</body>
</html>


What I have tried:

I m using html select Dropdownlist....i load the option values are dynamically ...and i m trying to load dropdown value like
Eg: $('#ddlCustType').val(data[0].Type); they not working....and the same time i m using the above code "alert("test"); " that time they correctly load...i don't know what's problem going...plz help..! thanks
Posted
Updated 29-Sep-16 6:05am
v2
Comments

if you want to set the value based on text then use this else val('value') should work fine.

JavaScript
var text = data[0].Type;
          $("#ddlCustType option").filter(function () {
              return this.text == text1;
          }).attr('selected', true);


keep an alert box for text and see whether you are getting the Id or Text or undefined.
 
Share this answer
 
I think what you are looking for is this.

To get selected text:

JavaScript
$("#ddlCustType option:selected").text();


Or to get selected value:

JavaScript
$("#ddlCustType option:selected").val();
 
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