2

I write Asp.Net MVC application and whant to realize cascading drop down lists functionality, but I have problem with jQuery change event. Where am I wrong?

Javascript:

<script charset="utf-8" type="text/javascript" language="javascript" src="<%= Url.Content("~/Content/jquery-1.4.1.js") %>"></script>
<script charset="utf-8" type="text/javascript" language="javascript">
  $(document).ready(function () {
      $("#ddlCategories").change(function () { alert("It worked!"); });
  });
</script>

MVC:

<%=Html.DropDownList("CategoriesId", (SelectList)ViewData["CategoriesList"], new { Id = "ddlCategories" })%>                    
<%=Html.DropDownList("ModelId", (SelectList)ViewData["ModelsList"], new { Id = "ddlModels" })%>  

HTML:

<select name="CategoriesId" id="ddlCategories">
  <option value="1">Thermage Solta Medical</option>
  <option value="2">Syneron</option>
  <option value="59">Deka</option>
</select>                    
<select name="ModelId" id="ddlModels">
  <option value="1">Thermage NXT RF</option>
  <option value="2">Thermacool TC</option>
</select>     
3

2 Answers 2

4

This may seem entirely obvious, and I know the question is old, but just in case someone reaches this page with the issue described by the initial poster, there is at least one possible cause that can happen easily when working with inherited code or on a large project.

Check to make sure that the ID of your select element is unique. I know IDs are supposed to be unique but since ASP.NET MVC will create the IDs for you if using the HtmlHelper classes, and because you may have many partials rendered in a single page, the chances of there being duplication is high. In my case I had 2 elements with the same ID, and so when I attached the event to $('#selectID') it was attaching to the first one, not the one I wanted it to.

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

Comments

1

works fine, see here. Are you sure that you don't have any other javascript errors?

Comments

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.