在局部视图中定义 jquery 就绪事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6967063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:37:48  来源:igfitidea点击:

defining jquery ready event in Partial View

jqueryasp.net-mvcpartial-viewsdocument-readydomready

提问by Safran Ali

I have defined a $(document).ready() event in Site.Master page and I also want to define another $(document).ready() in one of my partial view (which is use to display msgs and error msgs), and I am calling this partial view in all pages and all partial view ...

我在 Site.Master 页面中定义了一个 $(document).ready() 事件,我还想在我的一个局部视图中定义另一个 $(document).ready() (用于显示消息和错误消息) ,我在所有页面和所有部分视图中调用这个部分视图......

the partial views are displayed in the page and also using modal popup ... so I tried to this but the ready event in partial view is not firing

部分视图显示在页面中并使用模态弹出窗口......所以我尝试了这个但是部分视图中的就绪事件没有触发

I have few things to ask:

我有几件事要问:

  • first, is it possible to do what i am trying to do ...
  • there are pages which have partial view and because of it a page has two $(document).ready() events so when the page is loaded, is there be any clashes between these two events ...
  • 首先,是否有可能做我想做的事情......
  • 有些页面具有部分视图,因此页面具有两个 $(document).ready() 事件,因此在加载页面时,这两个事件之间是否存在任何冲突...

and if some body can provide wth some example ...

如果某个机构可以提供一些例子......

采纳答案by tvanfosson

Yes, you can include multiple ready event handlers on the page. You can put them in the site master, partial views, and view page itself -- as many as you need. They must all be enclosed in script tags. They will fire in the order that they are included in the final, rendered page. Note, you want to be careful to make sure that the partial is included only once on the page or that it doesn't matter if that handler is called multiple times.

是的,您可以在页面上包含多个就绪事件处理程序。您可以将它们放在站点主视图、部分视图和视图页面本身中——根据需要尽可能多。它们都必须包含在脚本标签中。它们将按照它们包含在最终呈现的页面中的顺序触发。请注意,您要小心确保部分在页面上只包含一次,或者多次调用该处理程序无关紧要。

Example (not complete):

示例(不完整):

Master:

掌握:

 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jqueryui.js"></script>
 <script type="text/javascript">
      $(function() {
           // do something for whole page
      });
 </script>

 @Html.Partial( "ErrorDialog" )

Partial (ErrorDialog)

部分(错误对话框)

 <div id="errorDialog" style="display: none;" title="Error">
     <p>An error occurred</p>
 </div>
 <script type="text/javascript">
      $(function() {
          $('#errorDialog').dialog({
             modal: true,
             autoOpen: false,
             // more options
          });
      });

      function showError(msg) {
          $('#errorDialog').find('p').html(msg)
                           .stop()
                           .dialog('open');
      }
 </script>

回答by Praveen Prasad

yes you are allowed to have multiple $(document).ready()in a page just make sure you have included jquery file before calling this function. Functionsinvoked inside $(document).ready()called in the order they are requested.

是的,您可以$(document).ready()在一个页面中有多个,只需确保在调用此函数之前已包含 jquery 文件。Functions在内部$(document).ready()调用,按照请求的顺序调用。

jQuery - multiple $(document).ready ...?

jQuery - 多个 $(document).ready ...?

回答by Jignesh Patel

Yep, that's right, as long as previous partial view does not have any error you can have as many $(document).ready() as you want and it will fire for all.

是的,没错,只要先前的局部视图没有任何错误,您就可以拥有任意数量的 $(document).ready() 并且它会为所有人触发。