简体   繁体   中英

$(document).ready not firing when returning view

I have a script that executes correctly when I first open my view. It enables and disables controls based on the values in my model.

$(document).ready(function () { ... });

After I submit my form,

        [HttpPost]
        public ActionResult Index(MyModel model)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    ...
                }
                catch (Exception ex)
                {
                    ...
                }
            }

            return this.View("Index", model);
        }

I want my script to be executed again, but it doesn't happen. How can I archieve this? I tried adding this script:

$("form").submit(function () {
        //execute code
    });

But to no avail. It will executed before my view is returned. I need to execute the script after my view is returned.

Make a function and put all your code in it.

<script>

         BindPageLoadEvents();  
         //This funciton will automatically call when your page loads first time, 
         //and also after your submit.  


    function BindPageLoadEvents(){
         ////Put all your page load (document.ready) events and binding here, in this method.
    }

<script>

I really don't know your real scenario but from your given explanation i hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM