首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ajax html响应无法在与javascript的链接上生效

ajax html响应无法在与javascript的链接上生效
EN

Stack Overflow用户
提问于 2012-05-27 22:32:41
回答 3查看 289关注 0票数 0

我制作了一个表单,用于向用户显示有关某些演示文稿的信息。我使用ajax是因为它应该加载到div容器中,而不是在更改年份时每次都重新加载。

这是我的JavaScript:

代码语言:javascript
复制
$("#select_coffee_talk_year").button().click(function() {
    var form = $('#coffee_talk_year');  
    var data = form.serialize();

    $.ajax({
        url: 'include/scripts/select_event.php',
        type: 'POST',
        data: data,
        dataType: 'html',
        success: function (select) {
            $("#coffee_talk").html(select);
            $("#coffee_talk").fadeIn();
        }   
    });
    return false;
});

这是我的select_event.php:

代码语言:javascript
复制
if ('POST' == $_SERVER['REQUEST_METHOD']) {
    /*******************************/
    /** Erzaehlcafe auswählen
    /*******************************/
    if (isset($_POST['coffee_talk_year_submit'])) {
        $getID = array();
        $getDate = array();
        $getTheme = array();
        $getContributer = array();
        $getBegin = array();
        $getPlace = array();
        $getEntrance = array();
        $getFlyer = array();

        $sql = "SELECT 
                    ID,
                    Date,
                    Theme,
                    Contributer,
                    Begin,
                    Place,
                    Entrance,
                    Flyer
                FROM 
                    Coffee_talk
                WHERE
                    YEAR(Date) = '".mysqli_real_escape_string($db, $_POST['year_coffee_talk'])."'
                ORDER BY 
                    Date ASC
                ";

        if (!$result = $db->query($sql)) {
            return $db->error;
        }

        while ($row = $result->fetch_assoc()) {
            $getID[$i] = $row['ID'];
            $getDate[$i] = $row['Date'];
            $getTheme[$i] = $row['Theme'];
            $getContributer[$i] = $row['Contributer'];
            $getBegin[$i] = $row['Begin'];
            $getPlace[$i] = $row['Place'];
            $getEntrance[$i] = $row['Entrance'];
            $getFlyer[$i] = $row['Flyer'];
            $i++;
        }

        $result->close();

        /****************************/
        /**  Output der Tabelle
        /****************************/
        if ($getID[0] == '') {
            echo 'Kein Eintrag vorhanden';
        } else {
            //Kopf
            echo '<table id="table">    
                        <thead>
                            <tr id="table_head">
                                <th>Nr.</th>
                                <th>Datum</th>
                                <th>Thema</th>
                                <th>Referent</th>
                                <th>Begin</th>
                                <th>Ort</th>
                                <th>Eintritt</th>
                                <th>Flyer</th>
                                <th>Bearbeiten</th>
                            </tr>
                        </thead>
                        <tbody>';

            //Mittelteil
            $j = 0;
            for($i = 0; $i < count($getID); $i++) {
                $j = $i + 1;
                echo '<tr>
                    <td class="center">'.$j.'</td>
                    <td class="center">'.$getDate[$i].'</td>
                    <td class="center">'.$getTheme[$i].'</td>
                    <td class="center">'.$getContributer[$i].'</td>
                    <td class="center">'.$getBegin[$i].'</td>
                    <td class="center">'.$getPlace[$i].'</td>
                    <td class="center">'.$getEntrance[$i].'</td>';
                    if ($getFlyer[$i] == '') {
                        //Kein Bild vorhanden
                        echo '<td class="center">Kein Bild vorhanden</td>';
                    } else echo '<td class="center">'.$getFlyer[$i].'</td>';
                    echo '<td class="center">
                        <table id="icons">
                            <tr>
                                <td>
                                    <a href="#" data-event-id="'.$getID[$i].'" data-table="Coffee_talk" class="edit_event">
                                        <img src="images/edit.png" />
                                    </a>
                                </td>
                                <td>
                                    <a href="#" data-event-id="'.$getID[$i].'" data-table="Coffee_talk" class="delete_event">
                                        <img src="images/delete.png" />
                                    </a>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>';
            }

            //Ende
            echo '</tbody>
                    </table>';
        }
    }
}

正如您所看到的,我的输出是一个表。我正在使用链接来实现编辑和删除功能:

代码语言:javascript
复制
<a href="#" data-event-id="'.$getID[$i].'" data-table="Coffee_talk" class="edit_event">
    <img src="images/edit.png" />
</a>

代码语言:javascript
复制
<a href="#" data-event-id="'.$getID[$i].'" data-table="Coffee_talk" class="delete_event">
    <img src="images/delete.png" />
</a>

这是我的html,带有占位符div:

代码语言:javascript
复制
<div>
    <p class="bold underline headline">Bereits eingetragen:</p>
    <form id="coffee_talk_year" action="include/scripts/select_event.php" method="post" accept-charset="utf-8"> 
        <select name="year_coffee_talk" id="year_coffee_talk">
            <option value="none" class="bold italic">Jahr</option>
            <?php
                for($i=2008; $i<=$year; $i++){
                    if ($i == $year) {
                        echo "<option value=\"".$i."\" selected=\"$i\">".$i."</option>\n";
                    } else  echo "<option value=\"".$i."\">".$i."</option>\n";
                }   
            ?>
        </select>
        &nbsp;&nbsp;
        <button id="select_coffee_talk_year">anzeigen</button>
        <input type="hidden" name="coffee_talk_year_submit" value="true" />​​​​​​​​​​​​​​​​​
    </form>
    <br />
    <div id="coffee_talk"></div>
    <br />
    <button id="add_coffee_talk">hinzufügen</button>
</div>

现在我想使用这个JavaScript来执行删除和编辑功能:

代码语言:javascript
复制
$(".delete_event").click(function() {
    alert('hallo');
    currentUserID = $(this).data("event-id");
    currentTable = $(this).data("table");
    $( "#dialog_delete_event" ).dialog( "open" );
});

$( '#dialog_delete_event' ).dialog({
    autoOpen: false,
    resizable: false,
    height: 170,
    modal: true,
    buttons: {
        'Ja': function() {
            document.location = "index.php?section=event_delete&id=" + currentUserID +"&table=" + currentTable;
            $( this ).dialog( 'close' );
        },
        'Nein': function() {
            $( this ).dialog( 'close' );
        }
    }
});

我的问题是click_function (在链接中定义)不工作。我发现ajax表单响应并没有写入html代码中。我敢打赌这就是问题所在。我该怎么做才能给这些链接一个有效的点击功能呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-27 22:43:21

我不确定由于你的代码的大小,但这是:

代码语言:javascript
复制
$(".delete_event").click(function() {

应该是

代码语言:javascript
复制
$(".delete_event").live("click",function() {

因为你是动态创建html的;"live“就是这样做的。

票数 1
EN

Stack Overflow用户

发布于 2012-05-27 22:43:40

像@Albit一样,假设ajax工作正常,并且结果在屏幕上可见,我不太确定你的问题是什么,而不是使用:

代码语言:javascript
复制
$(".delete_event").click(function() {

试一试

代码语言:javascript
复制
$(".delete_event").live("click", function() {

这意味着在页面加载后满足选择器的元素仍将触发事件。

票数 1
EN

Stack Overflow用户

发布于 2012-05-27 22:37:46

我不知道我是否正确地指出了你的问题,但是只要在锚的html标签中使用onclick事件,你就会好起来的。如果这不是正确答案,请忽略我

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10774729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档