不执行是点击事件

时间:2011-07-01 19:42:12

标签: jquery jquery-plugins

那么应该发生的事情是当用户点击是答案时它应该获取img的id然后执行其任务但是我不知道是否应该将id放到链接而不是图像上但问题的主要部分是它没有执行是点击功能的步骤。

我在这个页面上使用了相同的插件。 http://webstuffshare.com/demo/jConfirmAction/index.html 并使用默认用法的插件

<td> 
<a class="ask" href="#"> 
<img id="2" class="delete" border="0" title="" alt="" src="images/trash.png"> 
 </a>
 <div class="question" style="opacity: 1;">
  Are You Sure ?
   <br>
  <span class="yes">Yes</span>
  <span class="cancel">Cancel</span>
 </div>
</td>

$('.ask').jConfirmAction();

$('.yes').bind('click', function(){
    var userID = $(this).parent.attr('id'); 
    var dataString = 'userID=' + userID + '&deleteuser=True'; 
    $.ajax({ 
        type: "POST", 
        url: "processes/template.php", 
        data: dataString, 
    });
    $(this).parents("tr").eq(0).hide();   
}); 

3 个答案:

答案 0 :(得分:3)

您没有yes类的任何内容来绑定该点击。

编辑:(添加生成的代码后)

尝试将您的ID添加到<div class="question" style="opacity: 1;">,如果您无法将其添加到您的td并更改为var userID = $(this).parents('td').attr('id');

答案 1 :(得分:1)

好的,那么你应该在客户端(网络浏览器)中看到的是:

<td>
    <a href="#" class="ask">
        <img src="images/trash.png" class="delete" alt="" title="" border="0" id="some value" />
    </a>
</td>

所以,你给了你的图像一个id,但你的jQuery选择器试图将一个click事件绑定到超链接,并尝试解析一个表格单元格的ID 。正如其他人提到的那样,选择器不会选择任何内容,因为没有任何内容可以匹配它。

你知道我要去哪儿吗?

修改:

在图片上设置点击事件:

$(".delete").click(function(e){ 
    var id = e.target.id; 

    // ... the rest here
});

在超链接上设置它:

$(".ask").click(function(e){ 
    e.preventDefault();

    var id = $(this).("img").attr("id"); 

    // ... the rest here
});

编辑2:

如果你的目标是提供确认提示,我会按照K.I.S.S.原理;单击删除链接时使用JavaScript确认提示:

$(".ask").click(function(e){ 
    e.preventDefault();

    if(confirm("Are you sure?")){
        var id = $(this).("img").attr("id"); 

        // ... the rest here
    }
});

答案 2 :(得分:0)

在调用bind之前尝试使用alert("Some message");。将另一个作为function()的第一条指令。看看是否都达到了这两段代码。