2015-04-08 56 views
0

关于删除按钮点击我想显示确认对话框,并使用这个简单的plugin。按钮标记看起来像删除确认对话框不工作的第一次点击

<button type="button" class="btn contactDeleteRow" /> 

和我的JS貌似

$(document).on('click', '.contactDeleteRow', function() { 
$('.contactDeleteRow').easyconfirm(); 
$("#alert").click(function() { 
    // Actions to perform 
}); 
}); 

确认对话框,当我点击按钮,第一次没有出现,但正常工作之后的所有点击。任何想法,为什么它不会在第一次点击工作?而如何解决它

回答

2

您需要的点击只一次之前注册的插件
您在代码中所做的是在
之后点击注册它

$('.contactDeleteRow').easyconfirm(); // register before click 

$(document).on('click', '.contactDeleteRow', function() { 
    // do something here 
}); 

// dont register inside a click 
$("#alert").click(function() { 
    // Actions to perform 
}); 

这里有一个完整的HTML例子工程(把jquery.easy-confirm-dialog.js在同一目录中的HTML文件):

<!DOCTYPE html> 

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/blitzer/jquery-ui.css" type="text/css" /> 
    <script src="jquery.easy-confirm-dialog.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('.contactDeleteRow').easyconfirm({ locale: { title: 'This is my title', button: ['No', 'Yes'] } }); 
      $(document).on('click', '.contactDeleteRow', function() { 
       alert('you clicked me'); 
      }); 
     }) 
    </script> 
    <meta charset="utf-8" /> 
    <title></title> 
</head> 
<body> 
    <a href="#" class="contactDeleteRow"> 
     Click me 
    </a> 
</body> 
</html> 
0

你在你的代码的末尾缺少一些括号:

$(document).on('click', '.contactDeleteRow', function() { 
$('.contactDeleteRow').easyconfirm(); 
$("#alert").click(function() { 
// Actions to perform 
}); // <-- Missing brackets here... 
+0

道歉,我确实有括号却忘了把它们放在上面 – rumi