将数据传递到一个jQuery click()函数 [英] Passing data to a jQuery click() function

查看:173
本文介绍了将数据传递到一个jQuery click()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的跨度,像这样

I have a simple span like so

<span class="action removeAction">Remove</span>

这跨度是一个表中,每一行都有一个删除跨度。

This span is within a table, each row has a remove span.

然后,我使用AJAX跨度被点击时调用的URL。所述AJAX事件需要知道该行的对象的ID?是什么让这个ID进入点击功能的最佳方法是什么?

And then I call a URL using AJAX when that span is clicked. The AJAX event needs to know the ID of the object for that row? What is the best way of getting that ID into the click function?

我想我可以做这样的事情

I thought I could do something like this

<span class="action removeAction" id="1">Remove</span>

但是,一个ID不应该以数字开头?对?后来我想我可以做

But an ID should not start with a number? Right? Then I thought I could do

<span class="action removeAction" id="my1">Remove</span>

然后就脱光了'我'的一部分从ID,但是这似乎只是毓!

Then just strip the 'my' part from the ID, but that just seems Yuk!

下面是我的click事件,并在我的AJAX事件。

Below is my click event and where my AJAX event is.

<script type="text/javascript" language="text/javascript">

$(document).ready(function()
{

    $(".removeAction").click(function()
    {
        //AJAX here that needs to know the ID            
    }
});

</script>

我相信有这样做的一个很好的方式?

I am sure there is a nice way of doing this?

注:我不是在寻找

$(this).attr("id");

我希望能够通过多个资料片

I want to be able to pass more than one piece of information

感谢。杰克。

推荐答案

如果你坚持使用旧的学校HTML 4.01或XHTML:

If you insist on using old-school HTML 4.01 or XHTML:

$('.removeAction').click(function() {
 // Don’t do anything crazy like `$(this).attr('id')`.
 // You can get the `id` attribute value by simply accessing the property:
 this.id;
 // If you’re prefixing it with 'my' to validate as HTML 4.01, you can get just the ID like this:
 this.id.replace('my', '');
});

顺便说一句,在HTML5中, ID 属性可以以数字开头,甚至若干

By the way, in HTML5, the id attribute can start with a number or even be a number.

话又说回来,如果你使用的是HTML5,无论如何,你最好还是使用自定义数据属性,像这样:

Then again, if you’re using HTML5 anyway, you’re probably better off using custom data attributes, like so:

<span class="action removeAction" data-id="1">Remove</span>

这篇关于将数据传递到一个jQuery click()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆