javascript $(this).data()不起作用 [英] javascript $(this).data() not work

查看:266
本文介绍了javascript $(this).data()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一些使用ajax将值发送到数据库的代码。但是,我的警报框显示值为 undefined 。我不明白我的问题是什么。

I wrote some code that sends a value to a database using ajax. However, my alert box shows undefined for the value. I don't understand what my problem is.

<script>
    $(document).ready(function(){
        $(document).on('click', '#publish', function(){

            var status = $(this).data('data-pid');
            alert(status);
            $.ajax
            ({ 
                url: 'update_about_status.php',
                data: {"statusValue": status},
                type: 'post',
                success: function(result)
                {
                    $('.modal-box').text(result).fadeIn(700, function() 
                    {
                        setTimeout(function() 
                        {
                            $('.modal-box').fadeOut();
                        }, 2000);
                     });
                }
            });
        });
    });         

</script>
print'<button type="button" id="publish" class="btn btn-info" data-pid="'.$cd[$i][3].'">Make Publish</button>';


推荐答案

当你使用 data() api,无需使用数据 - 前缀来读取 data - * 属性值 - 请参阅 data-html5

When you use the data() api, there is no need to have the data- prefix to read the data-* attribute value - see data-html5

var status = $(this).data('pid');

如果您使用 .attr()然后你需要使用完整的属性名称,如

If you are using .attr() then you need to use the complete attribute name like

var status = $(this).attr('data-pid');

这篇关于javascript $(this).data()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆