如何将值从ruby后端传递到jquery确认对话框?

时间:2017-08-18 06:42:04

标签: jquery ruby sinatra

我正在构建一个带有Ruby后端的Web应用程序(主要是Sinatra,而不是Rails)。该应用程序用于管理机构,人员及其订阅。

当用户删除某些内容时,我想要自定义确认对话框。例如:

如果用户删除名为"公司A"的机构,我想说:Are you sure you want to delete Company A

如果他们删除名为" John Doe"的人,我想说:Are you sure you want to delete John Doe?

如果他们从John Doe删除了一个名为" Subscription A"的订阅,我想说:Are you sure you want to delete Subscription A from John Doe

我目前有一个基于jQuery的删除确认对话框,如下所示:

$('a.delete-action').click(function(event) {

    if (confirm("Are you sure you want to delete this?")) {
        return true;
    }

    return false;
});

关联的HTML和ERB是:

<p><a class="action-link delete-action" href="/institutions/<%= @institution.id %>/remove">Remove <%= @institution.name %></a></p>

如何将机构/人/订阅/所有这些内容的名称传递给jQuery对话框?

提前致谢!

1 个答案:

答案 0 :(得分:0)

将名称作为属性放在链接

<a href='...' data-name='John Doe'>...</a>

然后在点击处理程序中提取属性

$('a.delete-action').click(function(event) {
  var name = $(this).data('name')
  var prompt = "Are you sure you want to delete " + name + "?"

  if (confirm(prompt)) {
    return true;
  }

  return false;
});