简体   繁体   English

如何获取模态内的数据ID,然后将其传递给Ajax?

[英]How do I get the ID of a data inside a modal and then pass it to ajax?

I'm currently working on a system that has a CREATE EVENT module. 我目前正在使用具有CREATE EVENT模块的系统。 Now this CREATE EVENT module allows a user to create events and display it on their profile, and other users can join their event. 现在,此CREATE EVENT模块允许用户创建事件并将其显示在其个人资料上,其他用户也可以加入其事件。 I used a modal to view further details of an event posted. 我使用模态来查看发布的事件的更多详细信息。 In the perspective of the one creating the event, the user can publish an event to this so-called "PAST EVENTS" if the event is finished already. 从创建事件的角度来看,如果事件已经完成,则用户可以将事件发布到此所谓的“过去事件”。 I'm having trouble publishing since I cannot figure out how to pass an ID of an EVENT inside a modal. 由于无法弄清楚如何在模式内部传递事件的ID,因此发布时遇到了麻烦。

This is my modal. 这是我的模态。 I used ajax in retrieving and displaying the data. 我使用ajax检索和显示数据。

<div id="readmore" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title" id="event_title"></h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-6">
                        <img id="event_img">
                        <h6>HOW TO GET THERE</h6>
                    </div>
                    <div class="col-xs-6">
                        <p id="event_description"></p>
                        <h6>WHEN</h6>
                        <p id="event_start"></p>
                        <h6>WHO</h6>
                        <p id="occupation"></p>
                        <h6>WHAT TO BRING</h6>
                        <p id="event_material_req"></p>
                    </div>
                </div> 
            </div>
            <div class="modal-footer">
                <a class="btn btn-danger" data-dismiss="modal" onclick="publishToPast()">Publish to Past Activity</a>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

Now what I want is when this <a class="btn btn-danger" data-dismiss="modal" onclick="publishToPast()">Publish to Past Activity</a> will be clicked, it will get the ID of the data in the modal and then update the STATUS of the EVENT on the Database so it can now be displayed on the PAST EVENTS. 现在我要的是单击此<a class="btn btn-danger" data-dismiss="modal" onclick="publishToPast()">Publish to Past Activity</a>时,它将获得ID为以模态记录数据,然后更新数据库中事件的状态,以便现在可以在过去事件中显示它。 I was thinking getting it through PHP (through $_SESSION) but I cannot do so, because the modal depends on the number of EVENTS the user will create. 我当时正在考虑通过PHP(通过$ _SESSION)来获取它,但是我不能这样做,因为模式取决于用户将创建的事件数。

You have not mention but i guess that you are using an Ajax call to get and show event data in this modal. 您没有提及,但我想您正在使用Ajax调用来获取和显示此模式下的事件数据。 In this case, the way you are showing data of WHEN , WHO , WHAT TO BRING etc. You can add one hidden input field in this modal with some unique id like below and insert event id in that input field. 在这种情况下,您将以显示WHENWHOWHAT的数据等方式。您可以在此模式中添加一个隐藏的输入字段,并添加一些唯一的ID,如下所示,然后在该输入字段中插入事件ID。

<input type="hidden" name="event_id" id="event_id" value="">

Now in your publishToPast() function write below code. 现在,在您的publishToPast()函数中,编写以下代码。

var eventId = $("event_id").val();

Now you have event id in eventId variable. 现在,您在eventId变量中有了事件ID。 Use it wherever you want. 随时随地使用它。

Remember to remove event id from that input when modal close. 当模式关闭时,请记住从该输入中删除事件ID。

如果仅在onclick函数中显示模态时具有ID, onclick="publishToPast(herePassIDvalue)"

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

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