简体   繁体   English

如何将数据值传递给jQuery函数

[英]How to pass data- value to jquery function

I have this HTML line with combination of TWIG code: 我有结合了TWIG代码的HTML行:

<a href="#" class="aaa" data-redirect="{{ path('rezervace_smazat', {'terminId':rezervaceTabulka[i]['id_rezervace']}) }}"> delete </a>

The line is a part of cycle, so in final result it can be multiple with different value of data-redirect attribute. 该行是循环的一部分,因此最终结果可以是多个,具有不同的data-redirect属性值。

I need to pass the value of data-redirect to jquery function, but only a specific value when I click the hyper text link. 我需要将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"));
                        }
                });
        });
});

The function works fine except of the line, where I need to redirect to different URL. 除需要在其中重定向到其他URL的行外,该函数运行正常。 I need to pass to the window.location.assign() function the specific value from data-redirect attribute. 我需要将data-redirect属性中的特定值传递给window.location.assign()函数。

Actually $(this).attr("data-redirect") does not pass the value from the attribute. 实际上$(this).attr(“ data-redirect”)不会传递该属性的值。

Thank you for help. 谢谢你的帮助。

Try using. 尝试使用。 I hope this one helps. 希望这对您有所帮助。

$(this).data('redirect');

I believe is this is an issue. 我相信this是一个问题。 Sorry I just had to. 抱歉,我不得不。

What I mean is 我的意思是

...
onOk: function() {
  window.location.assign($(this).attr("data-redirect"));
                           ^----- may not be referring to what you think it is.
}
...

A quick solution would be to change to to an arrow function like so 一个快速的解决方案是像这样更改为箭头功能

onOk: () => { window.location.assign($(this).attr("data-redirect")); }

If you need better support coverage you could assign the variable this as a work around like so 如果您需要更好的支持范围,你可以在变量分配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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM