[英]How to pass data- value to jquery function
我有结合了TWIG代码的HTML行:
<a href="#" class="aaa" data-redirect="{{ path('rezervace_smazat', {'terminId':rezervaceTabulka[i]['id_rezervace']}) }}"> delete </a>
该行是循环的一部分,因此最终结果可以是多个,具有不同的data-redirect属性值。
我需要将data-redirect的值传递给jquery函数,但是单击超文本链接时仅将其传递给特定值。
$(document).ready(function() {
$('.aaa').on( "click", function(e) {
e.preventDefault();
$.confirm({
title: 'Smazat termín',
message: 'Opravdu chcete smazat tento termín?',
labelOk: 'Smazat',
labelCancel: 'Storno',
onOk: function() {
window.location.assign($(this).attr("data-redirect"));
}
});
});
});
除需要在其中重定向到其他URL的行外,该函数运行正常。 我需要将data-redirect属性中的特定值传递给window.location.assign()函数。
实际上$(this).attr(“ data-redirect”)不会传递该属性的值。
谢谢你的帮助。
尝试使用。 希望这对您有所帮助。
$(this).data('redirect');
我相信this
是一个问题。 抱歉,我不得不。
我的意思是
...
onOk: function() {
window.location.assign($(this).attr("data-redirect"));
^----- may not be referring to what you think it is.
}
...
一个快速的解决方案是像这样更改为箭头功能
onOk: () => { window.location.assign($(this).attr("data-redirect")); }
如果您需要更好的支持范围,你可以在变量分配this
作为工作周围像这样
$(document).ready(function() {
$('.aaa').on( "click", function(e) {
var self = this; // <--- here
e.preventDefault();
$.confirm({
title: 'Smazat termín',
message: 'Opravdu chcete smazat tento termín?',
labelOk: 'Smazat',
labelCancel: 'Storno',
onOk: function() {
window.location
.assign($(self)
.attr("data-redirect")); // <--- and here
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.