简体   繁体   English

将data属性与jquery一起使用时发生未定义的错误

[英]Undefined error when using data attribute with jquery

I'm using jquery to build a like and dislike on comments system, so I need jquery manipulation. 我正在使用jquery在评论系统上构建一个喜欢和不喜欢的东西,所以我需要jquery操纵。 To achieve this I'm using data attribute, but I get a undefined error with this code 为了实现这一点,我使用了data属性,但是此代码出现未定义的错误

I've been trying this for hours and I cant get it to alert the value of data attribute. 我已经尝试了好几个小时,但无法通知它data属性的值。

My javascript code: 我的JavaScript代码:

$('.unlike_link_comment').live("click", function(e){
    e.preventDefault();
    var likescount = jQuery(this).attr("data-likes");
    var newlikescount = parseInt(likescount) - 1;
    var commentid = jQuery(this).attr("data-id");
    var dislikescount = jQuery('#dislike_link_comment_4').data("dislikes");
    alert(dislikescount);
    $.ajax({
    url : 'alter_comments.php?action=unlike&comment_id='+commentid,
    beforeSend: function( xhr ) {
    xhr.overrideMimeType( 'text/plain; charset=UTF-8' );
  },
    success: function( data ) {
    $('#emptydiv_'+commentid).html(data);
    $('#like_count_'+commentid).html('<font color="red">'+newlikescount+' Likes</font>');
    $('#comment_control_'+commentid).html("<a data-likes='"+newlikescount+"' data-id='"+commentid+"' href='' class='like_link_comment'>Like</a> ");  
  }
    });    
    });

My HTML 我的HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div class='stbody' id='stbody_4'>
<div class='stimg'>
<img src='uploads/profile_pics_small/wood_texture_by_pabloalvin-d1igijr.jpg' /></img>
</div>
<div class="unlike_link_comment" style="width: 100px; height: 50px; margin: 0 auto; background-color:yellow">click me!</div>
<div class='sttext'><font color='black'>
sdasdasd
<div class='sttime'>By nicknick</div></font>
<br><br><font color='red'><p id='like_count_4' class='like_count'>0 Likes</p> <p id='dislike_count_4' class='dislike_count'>2 Dislikes</font></p> <div id='comment_control_4' class='commentcontrols'> <a data-likes='0' data-id='4' class='like_link_comment' id='like_link_comment_4' href=''>Like</a><a data-dislikes='2' data-id='4' class='dislike_link_comment' id='dislike_link_comment_4' href=''>Dislike</a></div> </div> </div>  <div id='emptydiv_4'> </div> </body> </html>

I myself am not very good at javascript, but when i ran it through a lint(javascriptlint.com), it gave the following error: 我自己不太擅长javascript,但是当我通过lint(javascriptlint.com)运行它时,出现了以下错误:

$('.unlike_link_comment').live("click", function(e){
    e.preventDefault();
    var likescount = jQuery(this).attr("data-likes");
    var newlikescount = parseInt(likescount) - 1;

===========================================^ lint warning: parseInt missing radix parameter =========================================== ^棉绒警告:parseInt缺少基数参数

maybe that's the problem? 也许那是问题吗?

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

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