【问题标题】:jquery functions not working within an ajax loaded jquery tabjquery 函数在 ajax 加载的 jquery 选项卡中不起作用
【发布时间】:2012-06-09 04:57:53
【问题描述】:

我无法让 jquery 函数(日期选择器和对话框)在 jquery ajax 加载选项卡中工作。

当我单击相应的选项卡时,我不断收到“日期选择器不是功能”或“对话框不是功能”。我为这个问题在网上做了很多搜索,也有类似的问题,但我找不到适合我的解决方案。

我明白,如果出现这样的错误,问题很可能是由于系统无法识别脚本。所以,在更高的层次上,我并不真正理解 ajax 加载的选项卡如何处理与主脚本相关的脚本。

希望我的代码可以帮助说明我的错误。

header.html

<head>
...
<!-- External javascript call -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" charset="utf-8"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" charset="utf-8"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/main.js"></script>    
</head>

Main.php

<?php include '../includes/header.html'; ?>
...
<div id="tabmenu" class="ui-tabs">
<ul>
<li><a href="view_sch.php"><span>Schedule</span></a></li>
</ul>
<div id="view_sch.php" class="ui-tabs-hide">Schedule</div>
</div><br />

view_sch.php(不包括 header.html)

<head>
<!-- External javascript call -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" charset="utf-8"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/schedule.js"></script>
</head>

<body>
<div id="AddGameForm" title="Add New Game">         
<form method="post">
<label for="date">Select Game Date:</label>
<input type="text" name="date" id="date" tabindex="-1" size="10" maxlength="10"    class="text ui-widget-content ui-corner-all" 
value="<?php if (isset($_POST['date'])) echo $_POST['date']; ?>" />
</form>
<table id="schedule"></table>
</body>

schedule.js

var GAME = {

loadDialog: function() {
    $( "#date" ).datepicker();
    $( "#AddGameForm" ).dialog({
        autoOpen: false,
        height: 400,
        width: 400,
        modal: true,
        buttons: {
            "Add New Game": function() {
                // Add game to database
                GAME.add();                     
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });

    $( "#add-game" )
        .button()
        .click(function() {
            $( "#AddGameForm" ).dialog( "open" );
    });     
},

add: function() { 
    var form_data = $('form').serialize();
    $.ajax({
        type: "POST",
        url: "../manager/add_game.php",
        data: form_data, // Data that I'm sending
        error: function() {
            $('#status').text('Update failed. Try again.').slideDown('slow');
        },
        success: function() {
            $('#status').text('Update successful!').slideDown('slow');
        },
        complete: function() {
            setTimeout(function() {
                $('#status').slideUp('slow');
                }, 2000);
        },
        cache: false
    });
  }
}

$(document).ready(function() {
// Load game dialog
GAME.loadDialog();
});

感谢您的帮助。

编辑:我应该注意,当您在自己的浏览器窗口中查看 view_sch.php 页面时,它可以正常工作。通过 ajax 加载的 jquery 选项卡查看页面时会出现问题。

【问题讨论】:

    标签: ajax jquery-ui-dialog jquery-ui-datepicker jquery-tabs


    【解决方案1】:

    我注意到您在文档就绪处理程序之前缺少 jQuery 对象。没有它,您的代码根本不会加载。

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

    或者更好

    $(function(){...})
    

    同样有效

    【讨论】:

      【解决方案2】:

      我想我想通了(在我朋友的帮助下)。我首先删除了 view_sch 页面中多余的外部脚本调用,因为 header.html 已经处理了这个问题。接下来,我利用 jquery 选项卡中的函数,特别是“加载”事件来创建回调情况。具有讽刺意味的是,如果您多次选择选项卡,直到我向加载块添加 if 条件,它仍然无法正常工作。希望下面的代码可以帮助其他人。

      schedule.js

      var GAME = {
       loadDialog: function() {
          $("#date").datepicker();
          $( "#AddGameForm" ).dialog({
              autoOpen: false,
              height: 400,
              width: 400,
              modal: true
              }
          });     
        }
      } 
      $(document).ready(function()
      {
        // jQuery UI Tabs
        $('#tabmenu').tabs({
          ajaxOptions: {
              error: function( xhr, status, index, anchor ) {
                  $(anchor.hash).html(
                      "Couldn't load this tab. We'll try to fix this as soon as possible. "
                  );              
              }
          },
          load: function ( event, ui ) {
              if (ui.index == 2) {
                  // Load game dialog
                  GAME.loadDialog();
      
                  $( "#add-game" ).on("click", function() {
                      $( "#AddGameForm" ).dialog( "open" ); 
                  }); 
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-19
        • 2015-09-23
        相关资源
        最近更新 更多