0

I have 2 functions called on $(document).ready() in my Main.js file:

$(document).ready(function () {
    UpdateValue();
    LocationValue();
});

UpdateValue() works fine, but when i call LocationValue(), it breaks. It seems to be due to the getWards() function called in LocationValue().

Here are the two functions:

function UpdateValue() {
$(document.body).on("change", ".Quantity", function () {
    var ProID = $(this).attr("data");
    var Quatity = $(this).val();
        $.ajax({
            type: "GET", url: "/Cart/UpdateValue", data: { ProID: ProID, quantity: Quatity },
            success: function (data) {
                $(".cart_box").html(data);
            }
        }
            );
        $.ajaxSetup({
            cache: false
        });
    });
}
function LocationValue() {
    $("#City").change(function () {
        var cityID = $("#City").val();
        alert(cityID);
        getWards(cityID);
    });
}
function getWards(cityID) {
    alert("in show");
    $.ajax({
        url: "/Checkout/Wards",
        data: {CityID: cityID},
        dataType:"json",        
        type:"POST",
        error:function()
        {
            alert("An error occured");
        },
        success:function(data)
        {
            var items="";
            $.each(data, function(i,item))
            {
            items+="<option value=\"" + item.Value + "\">" + item.Text + "</option>";
            });
            $("#Ward").html(items);
        }
    });
}

alert(cityID) in LocationValue() only shows correctly if I comment out the call to getWards(). What's wrong with my code?

5
  • 2
    What does "not working" mean? What errors do you get? Provide a line number if you can. Commented Jul 31, 2013 at 10:33
  • 1
    This question appears to be off-topic because it is about a syntax error. Commented Jul 31, 2013 at 10:35
  • UpdateValue() has extra }); Commented Jul 31, 2013 at 10:36
  • Not error, my js file is not working. Commented Jul 31, 2013 at 10:37
  • @Luffy What isn't working? What functionality is missing? Commented Jul 31, 2013 at 10:38

2 Answers 2

2
function getWards(cityID) {
    alert("in show");
    $.ajax({
        url: "/Checkout/Wards",
        data: {CityID: cityID},
        dataType:"json",        
        type:"POST",
        error:function()
        {
            alert("An error occured");
        },
        success:function(data)
        {
            var items="";
            $.each(data, function(i,item))

^^ You have one too many ')'... delete the last ) and it might work.

            {
            items+="<option value=\"" + item.Value + "\">" + item.Text + "</option>";
            });
            $("#Ward").html(items);
        }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it's my fault. I'm newbie in jquery, so i confused about {}().
We all have been there =) welcome aboard.. could you checkmark the answer as correct for historical sake?
0

You have an syntax error in the method

function getWards(cityID) {
    alert("in show");
    $.ajax({
        url : "/Checkout/Wards",
        data : {
            CityID : cityID
        },
        dataType : "json",
        type : "POST",
        error : function() {
            alert("An error occured");
        },
        success : function(data) {
            var items = "";
            $.each(data, function(i, item) { // an extra ) was here
                items += "<option value=\"" + item.Value
                + "\">" + item.Text + "</option>";
            });
            $("#Ward").html(items);
        }
    });
}

3 Comments

It works. But i don't understand why has varible i in function(i, item). "i" is not used.
@Luffy that is the syntax specification for the callback method of $.each(), those parameters are passed by the caller of the method - as a user we don't have control of that
$("#City").change(function () { var cityID = $("#City").val(); if (cityID == null) { $("Ward").prop("disabled", true); } else { $("Ward").prop("disabled", false); getWards(cityID); } }); I'm trying disable or return to Optional Label the second dropdown when the first dropdown has no value or in Optional Label. But this code doesn't work, i have set $("Ward").prop("disabled", true);

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.