1

jquery関数(datepickerとdialog)をjqueryajaxロードタブ内で機能させるのに問題があります。

それぞれのタブをクリックすると、「日付ピッカーは機能ではありません」または「ダイアログは機能ではありません」というメッセージが表示され続けます。私はこの問題をオンラインでたくさん検索しました、そして同様の問題があります、しかし私は私のために働く解決策を見つけることができませんでした。

このようなエラーが発生した場合、システムがスクリプトを認識しないことが問題である可能性が高いことを理解しています。したがって、より高いレベルでは、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タブからページを表示するときに発生します。

4

2 に答える 2

1

ドキュメントレディハンドラーの前にjQueryオブジェクトがないことに気づきました。それがないと、コードはまったく読み込まれません。

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

またはさらに良い

$(function(){...})

同様に機能します

于 2012-06-09T18:05:38.703 に答える
1

私はそれを理解したと思います(私の友人の助けも借りて)。私は最初にview_schページの冗長な外部スクリプト呼び出しを削除しました。header.htmlがすでにこれを処理しているからです。次に、jqueryタブ内の関数、特に「load」イベントを利用して、コールバック状況を作成しました。皮肉なことに、ロードブロックに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" ); 
            }); 
        }
    }
});
于 2012-06-10T22:38:36.317 に答える