我有多个链接:
<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的消息?
答案 0 :(得分:1)
.attr()
并获取href .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>