【问题标题】:Bootstrap Confirmation Dialog in PHPPHP 中的引导程序确认对话框
【发布时间】:2013-08-18 10:13:33
【问题描述】:

如何创建删除确认对话框?如果“是”点击删除消息,如果“否”点击取消删除操作。

目前我的看法是这样的:

<a href="javascript:;" class="btn btn-large btn-info msgbox-confirm">Confirm Message</a>

以及如何更改对话框内容?

【问题讨论】:

  • 我不太明白你的意思。您是在询问如何使用 JS/Jquery 或使用 PHP 使用页面重新加载或一般情况下开发流程或接受/拒绝? “更改对话框内容”是什么意思?点击后按钮消失?
  • 罗伯特,我只是问,如果是点击意味着我需要在控制器中调用一个方法,如果没有或取消点击意味着它不应该做任何事情。
  • PHP 中确认对话框的这段代码解决了我的问题。 '

    '&lt;a href="http://stackoverflow.com/" class="btn btn-large btn-info msgbox-confirm"onclick="return confirm('Are you sure want to delete the Code?');"&gt;Delete&lt;/a&gt;
  • no 和 cancel 有什么区别?只是问...

标签: php javascript jquery html css


【解决方案1】:

您不能在 PHP 中进行确认或引导确认,因为 PHP 是服务器端代码。

你将学习的是如何使用Javascript来创建一个确认框。

纯 Javascript

使用普通的标准javascript,这只需要一个带有功能的按钮

HTML

<button onclick="show_confirm()">Click me</button>

javascript

// function : show_confirm()
function show_confirm(){
    // build the confirm box
    var c=confirm("Are you sure you wish to delete?");

    // if true
    if (c){
        alert("true");
     }else{ // if false
        alert("false");
    }
  }

Demo jsFiddle

使用 jQuery 引导

这种方式稍微复杂一点,因为您要添加到 3rd 方库中。

首先,您需要创建一个模式来充当您的确认框。

HTML

<div id="confirmModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Delete?</h3>
  </div>
  <div class="modal-body">
    <p>Are you sure you wish to delete?</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
    <button onclick="ok_hit()" class="btn btn-primary">OK</button>
  </div>
</div> 

与以前相同的按钮,但有点样式

<button class="btn btn-danger" onclick="show_confirm()">Click me</button>

然后是

Javascript

// function : show_confirm()
function show_confirm(){
    // shows the modal on button press
    $('#confirmModal').modal('show');
}

// function : ok_hit()
function ok_hit(){
    // hides the modal
    $('#confirmModal').modal('hide');
    alert("OK Pressed");

    // all of the functions to do with the ok button being pressed would go in here
}

Demo jsFiddle

【讨论】:

    【解决方案2】:

    使用 javascript 的简单方法:

    <a onclick="if(!confirm('Are you sure that you want to permanently delete the selected element?'))return false" class="btn btn-large btn-info msgbox-confirm" href="?action=delete">Delete</a>
    

    这样,当用户点击“删除”时,对话框会要求她/他确认。如果它被接受,页面将使用 url 中的参数重新加载(更改为您需要的任何内容)。否则,对话框将关闭,什么也不会发生。

    【讨论】:

    • Rober,我需要上图的确认对话框样式,但上面的答案解决了一半问题
    【解决方案3】:
        <script>
    function deletechecked()
    { 
        var answer = confirm("Are you sure that you want to permenantly delete the selected element?")
        if (answer){
            document.messages.submit();
        }
    
        return false;  
    }
    </script> 
    
    <a href="<?php echo base_url('department/deletedept/'.$row['dept_id']);?>" onclick="return deletechecked();" title="Delete" data-rel="tooltip" class="btn btn-danger">
    

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      相关资源
      最近更新 更多