Solved

First click on a select box in MVC

Posted on 2010-08-26
3
486 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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 …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now