简体   繁体   English

如何使用jQuery将ID从MySQL发送到div?

[英]How to send ID from mysql to div using jquery?

On my page a couple of messages are retrieved from the database. 在我的页面上,从数据库中检索了一些消息。 They are displayed using fetch_array . 它们使用fetch_array显示。 On the corner of every message a button is shown. 在每条消息的角落都显示一个按钮。 This button contains the ID of the message. 此按钮包含消息的ID。 When it's clicked, a div will slide downwards from the top of the page, asking for a confirmation. 单击它时,div将从页面顶部向下滑动,要求确认。 If the yes-button is clicked, the message will be deleted. 如果单击是按钮,该消息将被删除。 If the no-button is clicked, the div slides upwards, off canvas. 如果单击无按钮,则div向上滑动,脱离画布。

How can I send the ID from the message to the yes-button, so the right message will be deleted instead of all messages? 如何将ID从消息发送到“是”按钮,这样正确的消息将被删除,而不是所有消息? I wrote this piece of code but this doesn't work. 我写了这段代码,但这行不通。

<? while($result=mysqli_fetch_array($sql,MYSQLI_ASSOC)){ ?>
    <div class="memo_blok">
        <table width="100%">
            <tr>
                <td>
                    <font class="memo_blok_name"><? echo $result['name']; ?> |</font> 
                    <font class="memo_blok_date"><? echo $result['date']; ?></font>
                </td>
                <td width="20">
                    <img src="images/close.png" width="20" id="deleteBTN_<? echo $result['id']; ?>" />
                </td>
            </tr>
        </table>
        <p class="memo_blok_TXT">
            <? echo $result['msg']; ?>
        </p>
    </div>
<? } ?>

<div id="confirm" class="confirm">
    <table width="100%">
        <tr>
            <td align="center">Are you sure?</td>
        </tr>
        <tr>
            <td align="center">
                <a href="memo_delete.php?id=1" class="noLine">
                    <img src="images/Yes.png" width="50" id="deleteYes" />
                </a>
                <img src="images/No.png" width="50" id="deleteNo" />
            </td>
        </tr>
    </table>
</div>

The CSS: CSS:

.confirm {
    width:100%;
    height:100px;
    position:fixed;
    top:-100px;
    left:0px;
    background-color:#3c3c3b;
    font-family: 'Abel', sans-serif;
    font-size:24px;
    color:#fefefe;
}

The script: 剧本:

<script> 
$(document).ready(function(){
    $("#deleteBTN").click(function(){
        $("#confirm").animate({top: '0px'});
    });

    $("#deleteNee").click(function(){
        $("#confirm").animate({top: '-100px'});
    });
});

Change the close image: 更改关闭图片:

<img src="images/close.png" width="20" id="deleteBTN_<? echo $result['id']; ?>" />

To this so it will have a data-id attribute (also you can change than the id attribute to class and leave out the id number): 为此,它将具有data-id属性(也可以将id属性更改为class,而忽略id号):

<img src="images/close.png" width="20" class="deleteBTN" data-id="<? echo $result['id']; ?>" />

Remove the value of href from a.noLine like this <a href="" class="noLine"> and in your javascript code then put the data attribute's value on the end of href attribute like this: 像这样<a href="" class="noLine">a.noLine移除href的值,然后在您的javascript代码中,将数据属性的值放在href属性的末尾,如下所示:

 $(".deleteBTN").click(function(){
     var msgId = $(this).data('id');
     $("#confirm").animate({top: '0px'});
     var $deleteBtn = $("#confirm").find('.noLine');
     $deleteBtn.attr('href','memo_delete.php?id='+msgId);
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM