一种将变量(记录ID)传递给jQuery事件侦听器的方法? [英] A way to pass variables (record ids) to jQuery event listeners?

查看:90
本文介绍了一种将变量(记录ID)传递给jQuery事件侦听器的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我通过PHP在我的网页上生成了一个记录列表,并且每个记录旁边都有一个链接.假设当我单击链接时会弹出一个模式窗口,然后在该模式中显示一个编辑表单.

Suppose I generate a list of records on my web page via PHP, and suppose each has a link beside it. And let's say that a modal window pops up when I click the link, and an edit form then shows in the modal.

我可以很容易地使这种事情发生:

I could easily make this happen like this:

<a href="javascript:showEditModal(12345)">Edit</a>

但是我更喜欢使用jQuery事件监听器:

But I'd prefer to use a jQuery event listener:

<a class="edit" href="#">edit</a>

$('.edit').click(function(event) {
  showEditModal(recordId);
});

如您所见,使用事件侦听器,我无法知道用户要通过模式编辑的记录.

As you can see, using an event listener, I do not have a way of knowing which record the user wants to edit via the modal.

如何允许用户指定要编辑的记录,同时仍将事件侦听器用于编辑链接?

How can I allow the user to specify which record to edit while still using an event listener for the edit links?

推荐答案

<a class="edit" href="#12345">edit</a>

$('.edit').click(function(e) {
  e.preventDefault();
  recordId = $(this).attr("href").substr(1);
  showEditModal(recordId);
  return false;
});

这篇关于一种将变量(记录ID)传递给jQuery事件侦听器的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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