繁体   English   中英

将变量从数据属性传递给onclick函数

[英]passing variable from data-attribute to onclick function

我有一个页面,其中包含许多记录的表

每行上都有一个按钮,我想添加一个onclick事件,以便触发一个弹出消息,如果他们确定,该页面将转移到另一个页面,并传递data属性中找到的值,但我无法使其正常工作

jQuery看起来像这样:

            var aFeatures = document.querySelectorAll(".sweet-roll");
        for (var i = 0; i < aFeatures.length; i++) {
                   aFeatures[i].onclick = function() {
                       swal({
                           title: "Rollback To Selected Version?",
                           text: "Are you sure you want to rollback to the selected version?",
                           type: "warning",
                           showCancelButton: true,
                           confirmButtonClass:  'btn-danger',
                           confirmButtonText:  'Yes, Rollback',
                           closeOnConfirm: false,
                           //closeOnCancel: false
                       },
                       function(){
                           window.location.href="/cms/rollback.aspx?id="+$(this).data('uuid');
                       });
                   };
               }
           };

这样,每个带有“ sweet-roll”类的按钮都将获得onclick事件。

每个html行如下所示:

<a class="btn btn-app sweet-roll" data-uuid="97aaa88c-ac9f-11e4-807b-0026b9ba6b95"><i class="fa fa-reply-all"></i>Rollback</a>

但是它一直在说该值是不确定的,好像找不到数据uuid值一样?

您的this引用的是SweetAlert调用者,而不是您的元素。 您需要做的是将其保存在某个变量中,例如_this

    var aFeatures = document.querySelectorAll(".sweet-roll");

    for (var i = 0; i < aFeatures.length; i++) {
               aFeatures[i].onclick = function() {
                   var _this = this

                   swal({
                       title: "Rollback To Selected Version?",
                       text: "Are you sure you want to rollback to the selected version?",
                       type: "warning",
                       showCancelButton: true,
                       confirmButtonClass:  'btn-danger',
                       confirmButtonText:  'Yes, Rollback',
                       closeOnConfirm: false,
                       //closeOnCancel: false
                   },
                   function(){
                       window.location.href="/cms/rollback.aspx?id="+$(_this).data('uuid');
                   });
               };
           }
       };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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