admin管理员组

文章数量:1332359

I am using dropdownlist helper in my razor view

@Html.DropDownList("Country", null, "Select Your Country", new { @class = "form-control", onchange = "clickMe()" })

I have placed my jquery file in the head section of Layout.cshtml

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Scripts.Render("~/bundles/jquery")
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>

But when I placed my script in a razor view

<script>
    $(document).ready(function () {
        function clickMe() { 
            alert();
        }
    });
</script>

it gives Reference error : clickMe() is undefined. When I try this code to popup an alert to check that my jquery file is loaded this works fine

<script>
    $(document).ready(function () {
        alert();
    });
</script>

I am using dropdownlist helper in my razor view

@Html.DropDownList("Country", null, "Select Your Country", new { @class = "form-control", onchange = "clickMe()" })

I have placed my jquery file in the head section of Layout.cshtml

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Scripts.Render("~/bundles/jquery")
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>

But when I placed my script in a razor view

<script>
    $(document).ready(function () {
        function clickMe() { 
            alert();
        }
    });
</script>

it gives Reference error : clickMe() is undefined. When I try this code to popup an alert to check that my jquery file is loaded this works fine

<script>
    $(document).ready(function () {
        alert();
    });
</script>
Share Improve this question edited Mar 5, 2015 at 22:09 Ghazanfar Khan asked Mar 5, 2015 at 21:21 Ghazanfar KhanGhazanfar Khan 3,7188 gold badges46 silver badges90 bronze badges 1
  • box() is a function which has nothing to do with the code you have shown - did you mean clickMe()? The code you have shown works fine. – user3559349 Commented Mar 5, 2015 at 21:38
Add a ment  | 

3 Answers 3

Reset to default 4

The actual reason this doesn't work is because of Scoping. The following code:

@Html.DropDownList("Country", 
  null, 
  "Select Your Country", 
  new { @class = "form-control", 
        onchange = "clickMe()" })

Yields something like (I hope):

<select onchange="clickMe()">
// this is looking for a global `clickMe()` function
// but is it available?
</select>

I've annotated your code here:

  // clickMe() is out of scope
  // it is not available here

  $(document).ready(function () 
  // start of anonymous function scope
  {

    function clickMe() 
    // start of clickme function scope
    { 
      alert();
    }
    // end of clickme function scope

    // clickMe() is available here
    // it was defined in this scope

  });
  // end of anonymous function scope

  // clickMe() is out of scope
  // it is not available here

</script>

I absolutely do not remend this, but to understand how you could make it work horribly, you could do the following:

<script>
  $(document).ready(function () 
  {
    window.clickMe = function()
    { 
      alert();
    }
  });
</script>

By assigning the function to the window object you make it globally available.

The better way to do it, taking advantage of Matt Bodily's Answer might look like:

<script>
    $(document).ready(function () {

        function clickMe() { 
            alert('made it!');
        }

        $(document).on('change', '#Country', clickMe );
    });
</script>

I much prefer putting the click events in the script. Try removing your on change event and replacing your function clickMe with

$(document).on('change', '#Country', function(){
    alert('made it!');
});

this selects on the ID of the dropdown which I believe will be country in the rendered drop down

Here is another option:

<script>
 $('#CountryID').change(function() {
    var value = $('#CountryID').val();


<script>

Just make sure you give your dropdown name= or id=

本文标签: javascriptRazor Dropdown onchange event not firing always undefinedStack Overflow