最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bootstrap Confirmation data-on-confirm not executing function - Stack Overflow

programmeradmin2浏览0评论

I'm trying to make a delete confirmation execute a function that deletes the current id data.

I am getting this error:

Uncaught TypeError: Cannot read property 'call' of undefined

in this part of the bootstrap-confirmation.js

return function() {
  context[func].call(this);
};

this is my button code

<button class="btn btn-danger" data-toggle="confirmation" 
data-    btn-ok-    class="btn-danger" data-btn-cancel-icon="glyphicon 
glyphicon-ban-circle" data-btn-cancel-class="btn-default" 
data-popout="true" data-singleton="true" data-on-confirm="goDel(id)">
Eliminar</button>

this is my Javascript

<script type="text/javascript">
function goDel(id)
{
location.href="./delete/" + id;
}
</script>

this is my delete controller

public function delete($id) {
    $clientes = new Clientes();
    $codcta = new Codcta();
    $this -> codcta = $codcta -> find();
    $codciu = new Codciu();
    $this -> codciu = $codciu -> find();        
    $clientes = new clientes();
    if ($clientes->delete((int)$id)) {
            Flash::valid('El registro se ha eliminado correctamente');
    }else{
            Flash::error('Falló Operación'); 
    }
    return Redirect::to();
}

I'm trying to make a delete confirmation execute a function that deletes the current id data.

I am getting this error:

Uncaught TypeError: Cannot read property 'call' of undefined

in this part of the bootstrap-confirmation.js

return function() {
  context[func].call(this);
};

this is my button code

<button class="btn btn-danger" data-toggle="confirmation" 
data-    btn-ok-    class="btn-danger" data-btn-cancel-icon="glyphicon 
glyphicon-ban-circle" data-btn-cancel-class="btn-default" 
data-popout="true" data-singleton="true" data-on-confirm="goDel(id)">
Eliminar</button>

this is my Javascript

<script type="text/javascript">
function goDel(id)
{
location.href="./delete/" + id;
}
</script>

this is my delete controller

public function delete($id) {
    $clientes = new Clientes();
    $codcta = new Codcta();
    $this -> codcta = $codcta -> find();
    $codciu = new Codciu();
    $this -> codciu = $codciu -> find();        
    $clientes = new clientes();
    if ($clientes->delete((int)$id)) {
            Flash::valid('El registro se ha eliminado correctamente');
    }else{
            Flash::error('Falló Operación'); 
    }
    return Redirect::to();
}
Share Improve this question edited Oct 21, 2015 at 15:44 maxshuty 10.7k13 gold badges69 silver badges86 bronze badges asked Oct 21, 2015 at 15:33 PongrPongr 411 silver badge7 bronze badges 8
  • There is no such "bootstrap confirmation" javascript plugin on the bootstrap project and there are quite several plugins named "bootstrap-confirmation" on github. Which one are you using? – Roberto Linares Commented Oct 21, 2015 at 16:00
  • Also, I don't think the data-on-confirm attribute can receive and call a function that requieres input parameters since it wouldn't know what to pass to the id parameter. Better use a function that doesn't receive any parameters and try to get the id using any other strategy. – Roberto Linares Commented Oct 21, 2015 at 16:04
  • @RobertoLinares I'm using this plugin, will try your solution and let you know if it works link – Pongr Commented Oct 21, 2015 at 16:08
  • @RobertoLinares tried to implement a session variable and got the same error. – Pongr Commented Oct 21, 2015 at 16:35
  • Can you update the source code on your question to reflect those changes? Also, a session variable is something stored on the server side of your application. All you are doing here occurs on the client without going to the server. – Roberto Linares Commented Oct 21, 2015 at 16:38
 |  Show 3 more ments

2 Answers 2

Reset to default 8

Change this in your button:

data-on-confirm="goDel"

and add this:

data-id="YOUR-ID-HERE"

in your javascript function:

function goDel()
{
    var id = $(this)[0].getAttribute('data-id');
    location.href="./delete/" + id;
}

Good Luck!

The button code in HTML will be:

<button class="btn btn-danger" data-toggle="confirmation" data-title="Title here" data-content="Content here" data-btn-cancel-icon="glyphicon 
glyphicon-ban-circle" data-btn-cancel-class="btn-default" 
data-popout="true" data-singleton="true" data-on-confirm="deleteMethodName" data-id="your-deleted-id-here">Eliminar</button>

The javascript function code will be:

function deleteMethodName()
{
    var id = $(this)[0].getAttribute('data-id');
    //Now you will get ID from above and use it as you like
}
发布评论

评论列表(0)

  1. 暂无评论