2017-09-08 15 views
-1

如何在JavaScript中获取帖子id。像Facebook的评论风格。 我需要提交评论后的所有帖子ID。 此代码只提交单个帖子。 我在一个页面中使用多文章。如何在php中使用id提交帖子

$(function() { 
    $(".comment_submit_button").click(function() { 

     var textcommets = $("#commets").val(); 
     var dataString = 'commets='+ textcommets; 
     if(textcommets=='') 
     { 
      alert("Enter some text.."); 
      $("#commets").focus(); 
     } 
     else 
     { 
      $("#flash").show(); 
      $("#flash").fadeIn(400).html('<span class="load">Loading..</span>'); 
      $.ajax({ 
       type: "POST", 
       url: "comment_action.php", 
       data: dataString, 
       cache: true, 
       success: function(html){ 
        $("#show").after(html); 
        document.getElementById('commets').value=''; 
        $("#flash").hide(); 
        $("#commets").focus(); 
       } 
      }); 
     } 
     return false; 
    }); 
}); 

<form method="post" name="form" action=""> 
    <textarea style="width:500px; font-size:14px; height:60px; font-weight:bold; resize:none;" name="commets" id="commets" ></textarea><br /> 
    <input type="submit" value="Post" name="submit" class="comment_submit_button"/> 
</form> 
</div> 
<div class="space"></div> 
<div id="flash" align="left" ></div> 
<div id="show" align="left"></div> 
+0

没有在你的HTML没有张贴ID就我所看到的。所以很难说如何获得帖子ID,因为它似乎不在那里。这是你真正的代码,还是仅仅是一个例子?我认为你需要让问题更清楚。 – ADyson

+0

这只是一个例子 –

+0

好吧,除非你展示一个更好的例子,否则我们无法真正地提供帮助。我们不知道这个帖子ID是你指的是什么,或者如何检索或使用它,因为我们不知道你从哪里获取价值。 – ADyson

回答

0

我想你谈论新的职位ID?添加后只需从服务器返回此值。像这样JSON:

{ 
    "postId":1, 
    "html": ..... 
} 

JS:

var postId = response.postId; 
$("#show").after(response.html); 
相关问题