dongqian6554 2018-04-09 07:01
浏览 155
已采纳

如何使用GET方法将id传递给同一页面中的模态?

I have a following link:

<a type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#product_view" href="?id='.$row['upcoming_event_id'].'">
                                    <i class="fa fa-search"></i> Read more</a>

The read more looks like this:

enter image description here

When I press the read more button, a modal will pop up like this:

enter image description here

I tried the following code but it's not working:

<div class="modal fade product_view" id="product_view">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <a href="#" data-dismiss="modal" class="class pull-right"><span
                            class="glyphicon glyphicon-remove"></span></a>
                <h3 class="modal-title">I want to show the event id here <?php echo $_GET['id']; ?></h3>
            </div>

            </div>
        </div>
    </div>
</div>
</div>

How to solve this? Or is there any better way to do this? I have to show the id into the modal, not on another page. Any help will be appreciated. Thanks in advance.

  • 写回答

3条回答 默认 最新

  • doudao3170 2018-04-09 07:14
    关注

    PHP code execute first, so you can't set PHP value for $_GET dynamically. Use a javascript function.

    function setEventId(event_id){
        document.querySelector("#event_id").innerHTML = event_id;
    }
    

    Call setEventId() function from anchor tag

    <a type="button" onclick="setEventId(<?= $row['upcoming_event_id'] ?>)" class="btn btn-primary" data-toggle="modal"
                                        data-target="#product_view" href="javascript:void(0)">
                                        <i class="fa fa-search"></i> Read more</a>
    

    And use <span id="event_id"></span> to replace html value with event_id

    <div class="modal fade product_view" id="product_view">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <a href="#" data-dismiss="modal" class="class pull-right"><span
                                class="glyphicon glyphicon-remove"></span></a>
                    <h3 class="modal-title">I want to show the event id here <span id="event_id"></span></h3>
                </div>
    
                </div>
            </div>
        </div>
    </div>
    </div>
    

    Here is demo example after getting value of $row['upcoming_event_id'] or page load.

    function setEventId(event_id){
        document.querySelector("#event_id").innerHTML = event_id;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <a type="button" onclick="setEventId(1)" class="btn btn-primary" data-toggle="modal"
                                        data-target="#product_view" href="javascript:void(0)">
                                        <i class="fa fa-search"></i> Read more</a>
                                        
    <a type="button" onclick="setEventId(2)" class="btn btn-primary" data-toggle="modal"
                                        data-target="#product_view" href="javascript:void(0)">
                                        <i class="fa fa-search"></i> Read more</a>
                                        
                                        
    <a type="button" onclick="setEventId(3)" class="btn btn-primary" data-toggle="modal"
                                        data-target="#product_view" href="javascript:void(0)">
                                        <i class="fa fa-search"></i> Read more</a>
                                        
    <div class="modal fade product_view" id="product_view">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <a href="#" data-dismiss="modal" class="class pull-right"><span
                                class="glyphicon glyphicon-remove"></span></a>
                    <h3 class="modal-title">I want to show the event id here <span id="event_id"></span></h3>
                </div>
    
                </div>
            </div>
        </div>

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?