【问题标题】:When rendering partial views how the jquery's $(document).ready() react?渲染部分视图时 jquery 的 $(document).ready() 如何反应?
【发布时间】:2011-05-19 11:55:06
【问题描述】:

在 $(document).ready() 中,我正在生成一个模型弹出窗口以在页面上添加项目,并且在第一次加载页面时它工作正常,但它不会再次显示模式弹出窗口它至少被调用一次,所以请告诉我我在哪里做错了它没有显示模态视图?

页面加载时jquery的ready()是否只调用一次?

这里是 jquery:

$(document).ready(function () {

            //select all the a tag with name equal to modal
            $('a[name=modal]').click(function (e) {
                //Cancel the link behavior
                e.preventDefault();

                //Get the A tag
                var id = $(this).attr('href');

                //Get the screen height and width
                var maskHeight = $(document).height();
                var maskWidth = $(window).width();

                //Set heigth and width to mask to fill up the whole screen
                $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

                //transition effect     
                $('#mask').fadeIn(1000);
                $('#mask').fadeTo("slow", 0.8);

                //Get the window height and width
                var winH = $(window).height();
                var winW = $(window).width();

                //Set the popup window to center
                $(id).css('top', winH / 2 - $(id).height() / 2);
                $(id).css('left', winW / 2 - $(id).width() / 2);

                //transition effect
                $(id).fadeIn(2000);

                // replacing text of divErrorMsg
                var htmlStr = $("#divErrorMsg").html();
                if (htmlStr != null && htmlStr.length > 0) {
                    htmlStr = null;
                    $("#divErrorMsg").text('');
                }
            });
       });

这是调用弹出窗口的链接:

<a name="modal" href="#iPopup" class="button smallButton">Add Item</a>

和 iPopup:

<div id="Popups">
        <div id="iPopup" class="popup">
        <a class="closeButton">x</a>
        <div class="popupContent">
          <h3>Choose a question type</h3>
                  <ul class="chooseQuestion">
             <li>
            <div class="short">
            <label>Question 1</label>
              <input />
              <p class="description">Eg. This is a description.</p> 
              </div>
                          <%= Ajax.ActionLink("Text", "action", new { id = tId }, new AjaxOptions() { UpdateTargetId = "tItems" }, new { @class = "button" })%>
                     </li>
                     <li>
            <div class="short">
            <label>Question 2</label>
              <input />
              <p class="description">Eg. This is a description.</p> 
              </div>
                          <%= Ajax.ActionLink("Text", "action", new { id = tId }, new AjaxOptions() { UpdateTargetId = "tItems" }, new { @class = "button" })%>
                     </li>
                  </ul>
            </div>
    </div>
        <div id="mask"></div>
    </div>

【问题讨论】:

    标签: jquery asp.net asp.net-mvc modal-dialog partial-views


    【解决方案1】:

    $(document).ready(function () {

    当页面准备好时,ready 语句中的任何块都会被调用。如果页面已经存在,它会被调用立即

    【讨论】:

    • 喜欢你对 $("stuff", "#partialviewcounter").doSomething() 的想法...谢谢
    【解决方案2】:

    是的,.ready() 在页面加载时被调用一次。只需使用.delegate() 注册您的点击处理程序,它就会动态获取由 AJAX 加载的新元素!

    http://api.jquery.com/delegate/

    * 编辑 *

    使用 Raynos 的想法,即 .delegate() 在某种程度上是邪恶的,因此每次在部分重新加载时更改 DOM 时,您都需要再次注册您的点击处理程序。为此,您必须找到在部分重新加载时执行的 JS 回调(假设有一个)并将所有原始代码放入其中:

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function (e) {
        ...
    });
    

    【讨论】:

    • @ChrisFrancis 您应该在添加元素时绑定事件。更好,
    • 好的,我看不出它有什么更好的地方......但在这种情况下,点击处理程序需要在部分回调中运行。
    • @Raynos 那么你建议在我的情况下做什么?如果您提供一些示例将会很有帮助
    • @dvlpr @ChrisFrancis 实际上使用委托是非常实用的。但是您可能希望通过 $("stuff", "#partialviewcounter").doSomething() 在部分视图中重新绑定一些事件
    猜你喜欢
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多