我有一个如此简单的跨度
@H_404_2@<span class="action removeAction">Remove</span>
这个跨度在一个表格内,每行都有一个删除跨度.
然后,当点击该跨度时,我使用AJAX调用URL. AJAX事件需要知道该行的对象的ID?将该ID转换为点击功能的最佳方式是什么?
我以为我可以做这样的事情
@H_404_2@<span class="action removeAction" id="1">Remove</span>但是ID不应该从数字开始?对?然后我以为我可以做
@H_404_2@<span class="action removeAction" id="my1">Remove</span>然后,从ID中剥离“我的”部分,但这只是哟!
以下是我的点击事件和我的AJAX事件.
@H_404_2@<script type="text/javascript" language="text/javascript"> $(document).ready(function() { $(".removeAction").click(function() { //AJAX here that needs to know the ID } }); </script>我相信有一个很好的方法来做到这一点
注意:我不在寻找
@H_404_2@$(this).attr("id");我想通过一个以上的信息
谢谢.杰克.
解决方法
如果您坚持使用旧版HTML 4.01或XHTML:
@H_404_2@$('.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,the id
attribute can start with a number or even be a number.
那么再说一次,如果你还在使用HTML5,那么你最好使用自定义数据属性,就像这样:
@H_404_2@<span class="action removeAction" data-id="1">Remove</span>