根据链接参数自定义onclick确认消息

时间:2019-07-03 23:48:15

标签: jquery confirm

我有多个链接:

<a href="validation/object_1/" class="validation">Validate</a>
<a href="validation/object_2/" class="validation">Validate</a>

object_n是动态定义的

我想请用户确认他要验证此对象

类似:

<script type="text/javascript">
$('.validation').on('click', function () {
    return confirm('Are you sure tou want to validate this "object_name" ?');
});

如何为第一个链接自定义对象名称为object_1的确认消息,为第二个链接自定义对象_2的消息?

1 个答案:

答案 0 :(得分:1)

  1. 使用.attr()并获取href
  2. 使用.split()生成所需的字符串

$('.validation').on('click', function() {
  return confirm('Are you sure tou want to validate this ' + $(this).attr('href').split("/")[1] + ' ?');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="validation/object_1/" class="validation">Validate</a>
<a href="validation/object_2/" class="validation">Validate</a>