douwen1006 2012-05-27 14:32
浏览 14
已采纳

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

I made a form for showing users informations about some presentations. I used ajax cause it should be loaded into a div container and not reloading each time while changing the year.

This is my 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;
});

This is my select_event.php:

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>';
        }
    }
}

As you can see my outpt is a table. I am using links to make an edit and delete function:

<a href="#" data-event-id="'.$getID[$i].'" data-table="Coffee_talk" class="edit_event">
    <img src="images/edit.png" />
</a>

and

<a href="#" data-event-id="'.$getID[$i].'" data-table="Coffee_talk" class="delete_event">
    <img src="images/delete.png" />
</a>

This is my html with the placeholder div:

<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>
";
                    } else  echo "<option value=\"".$i."\">".$i."</option>
";
                }   
            ?>
        </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>

Now I want to use this JavaScript to do a delete and edit function:

$(".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' );
        }
    }
});

My problem is that the click_function (defined in the links) is not working. I Found out that the ajax form response is not written into the html code. Bet that this is the problem. What can I do to give the links a working click function?

  • 写回答

3条回答 默认 最新

  • dtp19819 2012-05-27 14:43
    关注

    im not sure due to your code's size but this:

    $(".delete_event").click(function() {
    

    should be

    $(".delete_event").live("click",function() {
    

    because you dynamically create html; the "live" works with that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿