未触发Click事件

时间:2015-09-16 08:51:20

标签: jquery html html5

我已经对SO进行了研究,我尝试了建议的答案,但我的问题仍未解决!我想触发在click内动态插入的元素的div事件:

{% include "header.phtml" %}

<div id="choix_type_reservation" class="tile-area padding10">
    <div class="tile-container">
        <div id="choix_salles" class="tile-large bg-crimson fg-white">
            <div class="tile-content"><?php echo _getText("reservation.choix.salles"); ?>
                <div class="image-container">
                    <div class="frame"><img src="<?php echo HTTP_IMG ?>salle.jpg"/></div>
                </div>
            </div>
        </div>
        <div id="choix_tables" class="tile-large bg-darkPink fg-white">
            <div class="tile-content"><?php echo _getText("reservation.choix.tables"); ?>
                <div class="image-container">
                    <div class="frame"><img src="<?php echo HTTP_IMG ?>table.png"/></div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="liste_salles" class="tile-area padding10">
    <?php $nbSalles = 0; ?>
    <div style="font-size:20px;"><a href="#" data-role="icon-retour"><span class="mif-arrow-left mif-2x"></span></a></div><br />
    <div class="header fg-white"><?php echo _getText("reservation.choix.salles"); ?> ( <span id="stat_liste_salles_nb_salles"></span> )</div>
    <div class="tile-container" style="max-height: 324px;overflow: auto;">
        <?php
        $t_salle = new Salle();
        $salles = $t_salle->lireParCritere([]);
        foreach ($salles as $salle) {
            $nbSalles++;
        ?>
        <div id="salle_<?php echo $salle->salle_code; ?>" data-pk="<?php echo $salle->salle_code; ?>" class="tile-square bg-crimson fg-white">
            <div class="tile-content"><?php echo $salle->salle_lib; ?>
                <div class="image-container">
                    <div class="frame"><img src="<?php echo HTTP_IMG ?>salle.jpg"/></div>
                </div>
            </div>
        </div>
        <?php
        }
        ?>
    </div>
    <script type="text/javascript">
        var stat_liste_salles_nb_salles = "<?php echo $nbSalles; ?>";
        $('#stat_liste_salles_nb_salles').html(stat_liste_salles_nb_salles);
    </script>
</div>
<div id="liste_reservations_salle" class="tile-area padding10"></div>
<script type="text/javascript" charset="utf-8" language="javascript">

    $(document).ready(function() {

        $("#cell-content > div").hide(); // empty content-page

        $('#cell-content').addClass('bg-darkCobalt');

        $("#choix_type_reservation").show();

        <?php
        if (isset($from_reservation)) { // here I want to trigger the click event
        ?>
                from_reservation = "<?php echo $from_reservation; ?>";
                window.location.href = "../RestaurantReservation";
                $("#choix_salles").click(); // this doesnt work !
        <?php
        }
        ?>

        $("#choix_type_reservation").on("click", "#choix_salles", function(){
            previous_screen.push("#choix_type_reservation");
            $("#cell-content > div").hide();
            $("#liste_salles").show();
        });
        $("#choix_type_reservation").on("click", "#choix_tables", function(){
            previous_screen.push("#choix_type_reservation");
            $("#cell-content > div").hide();
            $("#liste_tables").show();
            listerTables($("#tile_salle_0").attr("data-pk"));
        });

    });
</script>

{% include "footer.phtml" %}

那有什么不对?

1 个答案:

答案 0 :(得分:2)

绑定事件处理程序后触发单击事件。您在事件处理程序绑定之前触发事件。

$(document).ready(function() {

    $("#cell-content > div").hide(); // empty content-page
    $('#cell-content').addClass('bg-darkCobalt');
    $("#choix_type_reservation").show();

    $("#choix_type_reservation").on("click", "#choix_salles", function(){
        previous_screen.push("#choix_type_reservation");
        $("#cell-content > div").hide();
        $("#liste_salles").show();
    });
    $("#choix_type_reservation").on("click", "#choix_tables", function(){
        previous_screen.push("#choix_type_reservation");
        $("#cell-content > div").hide();
        $("#liste_tables").show();
        listerTables($("#tile_salle_0").attr("data-pk"));
    });

    // Code moved to below event handler
    <?php
    if (isset($from_reservation)) { // here I want to trigger the click event
    ?>
            from_reservation = "<?php echo $from_reservation; ?>";
            window.location.href = "../RestaurantReservation";
            $("#choix_salles").click(); // this doesnt work !
    <?php
    }
    ?>

});