[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

First click on a select box in MVC

Posted on 2010-08-26
3
Medium Priority
?
502 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 2000 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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)
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 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