[英]How to use the attribute value of an element retrieved with jquery in html file?
I'm working on a bootstrap/flask website, which will display posts. 我正在一个bootstrap / flask网站上,它将显示帖子。 I have a dropdown menu on each post.
我在每个帖子上都有一个下拉菜单。 I need a way for retrieving the id of the post clicked (post.id) using jquery, and sending it to the server-side, so it can be saved in the database.
我需要一种使用jquery检索单击的帖子的ID(post.id)并将其发送到服务器端的方法,以便可以将其保存在数据库中。 First problem, i dont think my jquery script is correct, and second i don't know how to send it to the server side (site.py).
第一个问题,我认为我的jquery脚本不正确,第二个我不知道如何将其发送到服务器端(site.py)。
I've only attached some samples from my app, since its quite long and messy. 由于它很长而且很凌乱,所以我只附加了我应用程序中的一些示例。
UPDATE: I've updated the script using the script provided by Diptox below. 更新:我已经使用下面Diptox提供的脚本更新了脚本。 But it still saves the comment with post-id = 1, even though Im commenting on other posts.
但是,即使我在其他帖子上发表评论,它仍然保存post-id = 1的评论。
index.html - button sample index.html-按钮示例
`<button class="btn btn-default dropdown-toogle" aria-expanded="false" id="menu1" p-id= "{{post.id}}" data-toggle="dropdown" type="button">
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li>
<a name="comment" class="bound" data-toggle="modal" href="#" data-target="#modal_comment" >
<span class="glyphicon glyphicon-comment" aria-hidden="true"></span>
Comment
</a>
</li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="{{ url_for('DeleteIndexedPost', id=post.id) }}">{{ ('Delete') }}</a></li>
<li role="presentation">
<a role="menuitem" tabindex="-1" href="{{ url_for('MoveToArchive', id=post.id) }}">{{ ('Move to Archive') }} </a></li>
</ul>
index.html - modal and script sample index.html-模式和脚本示例
<div class="modal fade" id = "modal_comment" tabindex="-1" role="dialog" aria-labelledby="myModal" aria-hidden="true" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title" id="exampleModalLabel">Comment</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<form action="{{ url_for('comment', id=post.id) }}" role="form" method ="POST">
<input type="text" class="form-control" placeholder="Comment" name="comment" value="">
<input type="text" class="form-control" placeholder="ID" name="ID" value="">
<input class="btn btn-primary" type="submit" value="Submit">
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready( function()
{
$('#menu1').click(function()
{
var postid = $(this).attr("p-id");
$.post("/index/comment/<int:id>",{ postid: postid });
});
});
</script
site.py -sample site.py-样本
@app.route('/index/comment/<int:id>', methods=['GET','POST'])
def comment(id):
if request.method == 'POST':
new_comment = Comment()
new_comment.content= (request.form['comment'])
new_comment.workID= (request.form['ID'])
new_comment.post = id #This id is wrong (it defaults to 1 every time)
session.add(new_comment)
session.commit()
return redirect(url_for('index'))
return redirect(url_for('index'))
db.py -sample db.py-样本
class InfoPost (Base):
__tablename__ = 'Infopost'
id = Column(Integer, primary_key=True)
workerID = Column(String)
title = Column(String)
message = Column(String)
time = Column(DateTime)
comment = relation('Comment', backref='comment')
arkiv = Column(Boolean)
class Comment(Base):
__tablename__ = 'comment'
id = Column(Integer, primary_key=True)
workID = Column(String)
content = Column(String)
post = Column(Integer, ForeignKey('Infopost.id'))
here is the correct Jquery 这是正确的jQuery
$(document).ready( function()
{
$('#menu1').click(function()
{
var postid = $(this).attr("p-id");
$.post("/index/comment/<int:id>",{ postid: postid });
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.