Rez*_*eri 1 ajax jquery codeigniter
我有以下代码,我需要在jquery中获得帖子的ID!请帮帮我.
foreach($posts->result() as $post){
echo "<div class='post' id='$post->id' >";
echo $post->content;
echo "<div class='commentor' id='commentor' >";
echo "<input type='text' class='commentor_input' />";
echo "</div>";
echo "</div>";
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用下面的代码获取帖子的代码,但是不起作用!
$('.commentor_input').change(function(e){
e.stopImmediatePropagation();
var comment = $(this).val();
var post_id = $(this).closest("div").find(".post").attr("id");
alert(post_id); // alerts Undefined!
});
Run Code Online (Sandbox Code Playgroud)
使用 closest("div.post")
var post_id = $(this).closest("div.post").attr("id");
Run Code Online (Sandbox Code Playgroud)
closest返回与您的选择器匹配的第一个元素,因此它div.commentor在您的情况下返回,并且在其中.find找不到任何.post内容,因此undefined.
| 归档时间: |
|
| 查看次数: |
2054 次 |
| 最近记录: |