将数据传递给jQuery click()函数

前端之家收集整理的这篇文章主要介绍了将数据传递给jQuery click()函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个如此简单的跨度
  1. <span class="action removeAction">Remove</span>

这个跨度在一个表格内,每行都有一个删除跨度.

然后,当点击该跨度时,我使用AJAX调用URL. AJAX事件需要知道该行的对象的ID?将该ID转换为点击功能的最佳方式是什么?

我以为我可以做这样的事情

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

但是ID不应该从数字开始?对?然后我以为我可以做

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

然后,从ID中剥离“我的”部分,但这只是哟!

以下是我的点击事件和我的AJAX事件.

  1. <script type="text/javascript" language="text/javascript">
  2.  
  3. $(document).ready(function()
  4. {
  5.  
  6. $(".removeAction").click(function()
  7. {
  8. //AJAX here that needs to know the ID
  9. }
  10. });
  11.  
  12. </script>

我相信有一个很好的方法来做到这一点

注意:我不在寻找

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

我想通过一个以上的信息

谢谢.杰克.

解决方法

如果您坚持使用旧版HTML 4.01或XHTML:
  1. $('.removeAction').click(function() {
  2. // Don’t do anything crazy like `$(this).attr('id')`.
  3. // You can get the `id` attribute value by simply accessing the property:
  4. this.id;
  5. // If you’re prefixing it with 'my' to validate as HTML 4.01,you can get just the ID like this:
  6. this.id.replace('my','');
  7. });

顺便说一下,在HTML5,the id attribute can start with a number or even be a number.

那么再说一次,如果你还在使用HTML5,那么你最好使用自定义数据属性,就像这样:

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

猜你在找的jQuery相关文章