在 Ajax 提交后获取新的 Sql ID
Get a new Sql id after submition by Ajax
使用 ajax 提交帖子的评论后,评论.php显示带有自己的 SQL 自动增量 ID 的新评论。
// Here $id comes from SQL after submit data
<div class="comment'.$id.'"><ul>
// new comment
</ul></div>
现在,如何在Ajax中获取这个新id完成:函数(数据)从注释中.php显示它发布.php页面到.
我尝试了这段不起作用的代码:
complete: function(data){
var ID = $(this).attr('id').replace('comment','');
$(".comment"+ID).append(data.responseText);
$(".comment"+ID).fadeIn(2000);
}
完整代码:
$(".repfrm").click(function(){
error.fadeOut();
if(checkForm()){
var author = inputAuthor.attr("value");
var url = inputUrl.attr("value");
var img = inputImg.attr("value");
var replycom = inputReplycom.attr("value");
var parent_id = inputparent_id.attr("value");
var tutid = inputTutid.attr("value");
$.ajax({
type: "POST", url: "comment.php", data: "action=insert&author="+ author + "&replycom="+ replycom + "&url="+ url + "&img="+ img + "&parent_id="+ parent_id + "&tutid="+ tutid,
complete: function(data){
error.fadeOut();
var ID = $(this).attr('id').replace('comment','');
$(".comment"+ID).append(data.responseText);
$(".comment"+ID).fadeIn(2000);
});
}
});
}
else //alert("Please fill all fields!");
error_message();
});
它有助于准确描述您的代码如何不起作用,但我想我找到了它:
var ID = $(this).attr('id').replace('comment','');
应该是
var ID = $(this).attr('class').replace('comment','');
我想你需要这个:
这用于获取最后一个插入 ID,最后一个插入的记录的 ID
将其发送到您的 ajax 回调中并附加为 ID。
如果您想知道如何从评论中发送,请告诉我们.php
你的属性不是id
它是class
所以使用这样的东西:
var ID = $(this).attr('class').replace('comment','');
$(".comment"+ID).append(data.responseText);
$(".comment"+ID).fadeIn(2000);
有关示例,请参阅此内容:
<script>
$( document ).ready(function() {
var ID = $('div').attr('class').replace('comment','');
alert(ID);
});
</script>
</head>
<body>
<div class="comment1"></div>
</body>
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 对id以某个字符串开头的元素进行计数
- 如果元素's的ID以数字开头
- 通过id和class属性获取元素
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 使用当前日期生成随机id
- Javascript-ID冲突的几率
- Href:当前DIV中的目标ID
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- 如何将所有动态id的arrey值获取到javascript VAR CID,以便在sql中找到匹配项
- 当按下按钮时从SQL和PHP获取行内容,即使调用id
- 在 Ajax 提交后获取新的 Sql ID
- 如何使用 sql 行 id 使用使用 JSON 编码数据定义元素
- 如何从动态查询结果传递选定的sql行id到javascript
- 如何循环访问复选框 ID 并将其传递到 SQL DB