Solved

First click on a select box in MVC

Posted on 2010-08-26
3
492 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery Validation Messages Not Showing Up 3 27
Jquery form 9 39
Character counter breaks after adding EmojiOne Area 4 45
AJAX Wordpress Not Reading Variable 2 55
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

713 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