Solved

First click on a select box in MVC

Posted on 2010-08-26
3
490 Views
Last Modified: 2013-11-08
When using multiple items, clicking the first time on a select box within MVC does not fire off the change event in jQuery (using the onChange event of the select item works fine).

Only the second time I click on a value, I get a response.

What could this be?

                    <select id="MaterialsList" class="StockListBox" size="2">
                    <% foreach (var material in Model.Materials)
                       { %>
                         <option value="<%: material.Value %>"><%: material.Text %></option>
                    <% } %>                
                    </select>   

Open in new window

           

My jQuery code:

    $(document).ready(function () {

        $("#MaterialsList").change(MaterialsListChanged);

        //////////////////////////////////////
        function MaterialsListChanged() {
            alert('ok');
            $("#ModelsList > option").remove();
            var materialId = $("#MaterialsList option:selected").val();
            $.post("/Stock/LoadBrands/" + materialId, null, function (data) {
                data = $.map(data, function (item, a) {
                    return "<option value=" + item.Value + ">" + item.Text + "</option>";
                });
                $("#BrandsList").html(data.join(""));
            });
        }

Open in new window


Thx in advance

Gerrie
0
Comment
Question by:gerrie-govaerts
  • 2
3 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 33532785
Here's a snippet that worked on machine.

Arun

    <select id="MaterialsList" class="StockListBox" size="2" onchange="MaterialsListChanged();">
        <% foreach (var material in Model)
           { %>
        <option value="<%= material.Value %>">
            <%= material.Text %></option>
        <% } %>
    </select>
    
    <script language="javascript" type="text/javascript">
        function MaterialsListChanged() 
        {
            alert('ok');
        }
    </script>

Open in new window

0
 
LVL 27

Accepted Solution

by:
nmarun earned 500 total points
ID: 33532860
And the reason you're having that behavior is because the event will be fired only when the selected item is changed. This does not seem to include the first click. If you want to fire the event on every click (including the first one), then capture the 'click' event and not the change.
Arun

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $("#MaterialsList").click(MaterialsListChanged);
    });

    function MaterialsListChanged() 
    {
        alert('ok');
    }
</script>

Open in new window

0
 

Author Closing Comment

by:gerrie-govaerts
ID: 33532940
Hello

Your first solution isn't working because I'm dealing with jQuery functions and the onChange event of the listbox can't reach that one apparently.

The second solution however is correct, capturing the Click event instead of the Change event has helped me!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 79
jQuery Validate 4 36
How to make a moving character 3 55
Scroll 5 news at a time. 4 25
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question