2

I'm developing Asp.Net MVC application. I'm using partial view for menu in Layout form for some views. I need use JavaScript on this view, but its work only on first time - after view loaded. Later - doesn't. Partial View is like this:

@Scripts.Render("~/bundles/jquery")
@Html.CheckBox("IsChoosed", true, new { onclick = "FormReload();" })

<script>
    $(".IsChoosed").on("onchange", function () {
        FormReload();
    });

    var FormReload = new function ()
    {
        alert("FormReload");
    };

</script>

LayoutForm:

@Html.Partial("_ViewPartial")

@RenderBody()

Alert message is displayed after loading of page, when checkbox value set. But after while i'm changing value - doesn't. I tried to move JavaScritp methods to ***.js file and hook up it in view, but its worked the same way. Where can be an error?

2 Answers 2

1

Your change event is not firing because you are trying to attach to the class .IsChoosed.

try changing your change handler to:-

$("#IsChoosed").on("onchange", function () {
     FormReload();
});

so #IsChoosed instead of .IsChoosed.

EDIT

Also, change:-

var FormReload = new function ()

to this:-

function FormReload()
Sign up to request clarification or add additional context in comments.

3 Comments

ieaglle, BG101, thank you very much! I changed code like you told, but its still don't work. Also it strenge that alert message is displayed after pade loading and later not.
If they helped, give them an upvote and mark the relevant answer as correct :^)
BG101, i'm sorry, it didn't help.
0

@Html.CheckBox("IsChoosed",... - first argument is name.

Selector $(".IsChoosed") looks for class IsChoosed. So onchange callback is not set.

To fix you can do the following:

@Html.CheckBox("IsChoosed", true, new { @class="IsChoosed", onclick = "FormReload();" })

1 Comment

ieaglle, thank you very much! I changed code like you told, but its still don't work. Also it strenge that alert message is displayed after pade loading and later not

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.