javascript $(this).data()不起作用

编程入门 行业动态 更新时间:2024-10-17 00:20:52
本文介绍了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()不起作用

本文发布于:2023-11-26 20:41:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   javascript   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!