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

如果单击包含“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.

好的所以我使用的bootbox工作正常,但是当我想用PHP链接发送变量时 javascript工作但不是bootbox。 p>

----这有效---- p>

 &lt; a href ='#'class =“alert_without_href”&gt; 我没有href&lt; / a&gt; 
&lt; script&gt; 
 $(document).ready(function()
 {
 $('a.alert_without_href')。on('click',function()\  n {
 alert('This works'); 
 bootbox.alert(“此ALSO有效”); 
}); 
}); 
&lt; / script&gt; 
  code>  pre  > 
 
 

----这不起作用---- p>

 &lt; a href =“?name = Jacob”class =“alert_with_href  “&gt;我有一个href&lt; / a&gt;&lt; br&gt;&lt; br&gt; 
&lt; script&gt; 
 $(文件).ready(function()
 {
 $('a.alert_with_href')。  on('click',function()
 {
 alert('This works'); 
 bootbox.alert(“This one DOESNT work”);&lt; ----问题在这里
警报(' 这也适用'); 
}); 
}); 
&lt; / script&gt; 
  code>  pre> 
 
 

----然后最终显示 - - p>

 &lt;?php 
 if($ _SERVER ['REQUEST_METHOD'] ==='GET')
 {
 echo'Hello'。  $ _GET [“name”]; 
} 
?&gt; 
  code>  pre> 
 
 

我确实需要在链接中传递一个变量,它不能被取出。 我该如何解决这个问题? p>

提前致谢 p>

----编辑---- 我添加了下表的图像 关于究竟要做什么的解释 p>

用户表 p>

- 只有在您看到图像时才会读取 - 因此,表格中“操作”下的每个链接都会创建一个包含用户唯一ID变量的链接。 单击它时,我检查是否设置了$ _GET ['remove_id']然后运行MySQL查询以删除具有该ID的人。 p>

所以基本上是Bootbox的想法是 如果你想删除用户,我不想使用正常的警报(''),因为可以禁用它。 p>

ps 删除功能确实有效,我只想使用bootbox添加确认。 p> div>

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