doulierong0334 2016-08-13 10:37 采纳率: 100%
浏览 79
已采纳

如果单击包含“href”的链接,则不会显示Bootbox模式

Ok so I am using bootbox which works perfectly, but when I want to send variables in a link with PHP then all javascript works but not the bootbox.

----This works----

<a href='#' class="alert_without_href">I dont have an href</a>
<script>
$(document).ready(function()
 {
    $('a.alert_without_href').on('click', function()
    {
        alert('This works');
        bootbox.alert("This ALSO works");
    });
});
</script>

----This doesn't work----

<a href="?name=Jacob" class="alert_with_href">I have an href</a><br><br>
<script>
   $(document).ready(function()
   {
      $('a.alert_with_href').on('click', function()
      {
        alert('This works');
        bootbox.alert("This one DOESNT work"); <---- PROBLEM IS HERE
        alert('This also works');
      });
   });
</script>

----And then finally to display----

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'GET')
    {
        echo 'Hello ' . $_GET["name"];
    }  
?>

I do need to pass a variable in the link, it cannot be taken out. How can I fix this?

Thanks in advance

----EDIT---- I added an image of a table below with an explanation of what exactly has to be done

Table of users

--ONLY READ IF YOU SAW THE IMAGE-- So each link under "Action" in the table creates a link with a variable of the user's unique ID. When it is clicked I check if $_GET['remove_id'] is set and then run a MySQL query to delete that person with that ID.

So basically the idea of Bootbox is to be a confirmation message if you want to delete the user, I don't want to use normal alert('') because that can be disabled.

p.s. the delete function does work, I just want to add a confirmation now using bootbox.

  • 写回答

1条回答 默认 最新

  • dongliao8069 2016-08-14 04:23
    关注

    Bootstrap (and therefore Bootbox) modals do not generate blocking events, so if you want something to happen when you click on a hyperlink, you need to prevent the default behavior (page navigation). Something like:

    <a href="?name=Jacob" class="alert_with_href">I have an href</a><br><br>
    <script>
       $(document).ready(function() {
          $('a.alert_with_href').on('click', function(e) // <-- add an event variable
          {
            e.preventDefault(); // <-- prevent the event
            bootbox.alert("I am an alert!");
          });
       });
    </script>
    

    Since you said you want to confirm something, use bootbox.confirm and some AJAX:

    <a href="?name=Jacob" class="alert_with_href" data-name="Jacob">I have an href</a><br><br>
    <script>
        $(document).ready(function() {
            $('a.alert_with_href').on('click', function(e) // <-- add an event variable
            {
                e.preventDefault(); // <-- prevent the event
    
                var data = { name = $(this).data('name') };
                bootbox.confirm("Really delete?", function(result){
                    if(result){
                        $.post('your-url', data)
                            .done(function(response, status, jqxhr){
                                // do something when successful response
                            })
                            .fail(function(jqxhr, status, error){
                                // do something when failure response
                            });
                    }// end if
                }); // end confirm
            });
        });
    </script>
    

    I also added a data-attribute to make it easier to get the value for posting (don't use a GET request to perform a delete action). I would assume you would reload the page on a successful delete, or you can use .remove to remove the "row" containing the item you're removing

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值