当我第一次打开我的 View 时,我有一个脚本可以正确执行。它根据我模型中的值启用和禁用控件。
$(document).ready(function () { ... });
提交表单后,
[HttpPost]
public ActionResult Index(MyModel model)
{
if (this.ModelState.IsValid)
{
try
{
...
}
catch (Exception ex)
{
...
}
}
return this.View("Index", model);
}
我希望再次执行我的脚本,但它没有发生。我怎样才能做到这一点?我尝试添加这个脚本:
$("form").submit(function () {
//execute code
});
但是没有用。它会在我的 View 返回之前执行。我需要在我的 View 返回后执行脚本。
最佳答案
创建一个函数并将所有代码放入其中。
<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>
我真的不知道您的真实情况,但根据您给出的解释,我希望这对您有所帮助。
关于javascript - $(document).ready 返回 View 时不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33548223/