0

I have a dropdown that has a list of items, the first item has a value of 0, the other items a value greater than 0.

The following code is attached to the change event:

<script type="text/javascript">
    $("#SearchRegionId").change(function (e) {
        var select = $("#SearchKommuneId");
        select.empty();
        if ($("#SearchRegionId").val() != 0);
        {
            $.ajax({
                url: '/Kommune/getKommunerIRegion/',
                type: 'POST',
                data: { RegionId: $("#SearchRegionId").val() },
                dataType: 'json',
                success: function (data) {
                    for (i in data) {
                        select.append($('<option value="' + data[i].KommuneId + '">' + data[i].KommuneNavn + '</option>'));
                    }
                }
            });
        }
    });
</script>

My problem is that when I test to see if $("#SearchRegionId").val() != 0 it always comes out true even if the selected value is 0. I have showed the value in an alert box that shows the value 0, but something tells me that it is not really 0.

1
  • select.empty(); why that? Commented Apr 8, 2013 at 7:21

2 Answers 2

1

You have a ; at the end of if

if ($("#SearchRegionId").val() != 0)

The ; at the end of ; means that the if condition is ended there and the code block after that will get executed irrespective of the value of the condition.

<script type="text/javascript">
    $("#SearchRegionId").change(function (e) {
        var select = $("#SearchKommuneId");
        select.empty();
        if ($("#SearchRegionId").val() != 0)
        {
            $.ajax({
                url: '/Kommune/getKommunerIRegion/',
                type: 'POST',
                data: { RegionId: $("#SearchRegionId").val() },
                dataType: 'json',
                success: function (data) {
                    for (i in data) {
                        select.append($('<option value="' + data[i].KommuneId + '">' + data[i].KommuneNavn + '</option>'));
                    }
                }
            });
        }
    });
</script>

Demo: Problem
Demo: Solution

Sign up to request clarification or add additional context in comments.

1 Comment

Arun P Johny, Ooooh my ...., great thanks for your answer. I need a trip in the sun and wind I think :-D
0

Why you write

select.empty();

after you assign the value, then you check it.....

It makes the

var select;

become empty, isn't it? That's what I think, same as roXon

1 Comment

Please note that I have two dropdowns in a drilldown scenario: One dropdown is for municipalities: var select = $("#SearchKommuneId"); select.empty(); One is for Regions, this is the dropdown that fires the change event: if ($("#SearchRegionId").val() != '0');

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.