1

嗨,我是 jquery 的新手...

我的项目中几乎没有锚链接...

<a href="#" id="1" class="delete" onclick="dele()"> Remove1 </a>
<a href="#" id="2" class="delete" onclick="dele()"> Remove2 </a>
<a href="#" id="3" class="delete" onclick="dele()"> Remove3 </a>
<a href="#" id="4" class="delete" onclick="dele()"> Remove4 </a>

function dele()
    {
        fid= //here i want id of that element which called it
        $.ajax({
            url : 'ajax.php',
            data : {delet:'delet',fid:fid},
            type : 'POST',
            success : function(data)
            {
                $("#showform").html(data);
                    alert(data);
            }
        });
    }

例如

如果我点击Remove1然后我想要它的id1

如果我点击Remove2那么我想要它的id2等等......

我试图通过$(".delete").click()来做到这一点,但我不能使用它,因为它给我的项目带来了问题......

这里 id 我通过数据库生成...

你能建议我如何获得身份证吗???

提前致谢..

4

4 回答 4

2

尝试这个

<a href="#" id="1" class="delete" onclick="dele(this.id)"> Remove1 </a>

脚本

function dele(id)
    {
        fid= id //here your id
        $.ajax({
            url : 'ajax.php',
            data : {delet:'delet',fid:fid},
            type : 'POST',
            success : function(data)
            {
                $("#showform").html(data);
                    alert(data);
            }
        });
    }

演示

于 2013-11-14T12:22:04.593 回答
1

既然您将 jQuery 用于您的逻辑,为什么不使用它来绑定事件并将 JS 与 HTML 完全分开呢?尝试这个:

<a href="#" id="1" class="delete"> Remove1 </a>
<a href="#" id="2" class="delete"> Remove2 </a>
<a href="#" id="3" class="delete"> Remove3 </a>
<a href="#" id="4" class="delete"> Remove4 </a>
$('.delete').click(function() {
    fid = this.id; // this = the element which was clicked on
    $.ajax({
        url : 'ajax.php',
        data : { delet: 'delet', fid: fid },
        type : 'POST',
        success : function(data) {
            $("#showform").html(data);
                alert(data);
        }
    });
});

我不能使用 $(".delete").click()... bcz 当我从 php 文件返回响应时,它会影响我的功能.. 我使用的是相同的,但是在我看到我的功能不起作用之后我正在使用这种方式

如果您.delete在 DOM 加载后添加元素,则需要修改上述内容以使用委托选择器:

$(document).on('click', '.delete', function() {
   // rest of the code...
});
于 2013-11-14T12:23:35.947 回答
0

尝试这个

 <a href="#" id="1" class="delete" onclick="dele(this)"> Remove1 </a>
 <a href="#" id="2" class="delete" onclick="dele(this)"> Remove2 </a>
 <a href="#" id="3" class="delete" onclick="dele(this)"> Remove3 </a>
 <a href="#" id="4" class="delete" onclick="dele(this)"> Remove4 </a>


function dele(element)
{
    fid= element.id;
    $.ajax({
        url : 'ajax.php',
        data : {delet:'delet',fid:fid},
        type : 'POST',
        success : function(data)
        {
            $("#showform").html(data);
                alert(data);
        }
    });
 }

如果你传递元素,而不是只有 id,你还可以获得元素的一些其他属性。

于 2013-11-14T12:28:41.867 回答
0

试试喜欢

function dele()
{
    var fid= $(this).attr('id');
    $.ajax({
        url : 'ajax.php',
        data : {delet:'delet',fid:fid},
        type : 'POST',
        success : function(data)
        {
            $("#showform").html(data);
                alert(data);
        }
    });
}

甚至您也可以直接从调用者函数中发送 id,例如

<a href="#" id="4" class="delete" onclick="dele(this.id)"> Remove4 </a>
于 2013-11-14T12:22:38.150 回答