Javascript 获取HTML元素的name属性并将其发送到服务器
我正在尝试将元素的name属性发送到服务器 因此,用户会单击一个元素,我会将其名称发送到服务器。 我用的是烧瓶 现在我有一个JS函数,它选择元素名称并将其存储在一个变量中:Javascript 获取HTML元素的name属性并将其发送到服务器,javascript,python,jquery,html,flask,Javascript,Python,Jquery,Html,Flask,我正在尝试将元素的name属性发送到服务器 因此,用户会单击一个元素,我会将其名称发送到服务器。 我用的是烧瓶 现在我有一个JS函数,它选择元素名称并将其存储在一个变量中: $(document).ready(function(){ $("a").click(function(){ $(this).hide(); let x = $(this).attr('name'); console.log(x); }); }); 以下是我在服务器上的代码: @app.
$(document).ready(function(){
$("a").click(function(){
$(this).hide();
let x = $(this).attr('name');
console.log(x);
});
});
以下是我在服务器上的代码:
@app.route('/friends_Profile', methods=["GET"])
def friends_profile():
name = request.args.get('x')
print(name)
return render_template('friends-profile.html', friend=name)
名称变量为空(无)
以下是HTML:
<div class='friends'>
<b>You are Friends with:</b>
{% for x in range(friends|length) %}
<li><a id='{{loop.index}}'
method="GET" href="/friends_Profile" name='{{friends[loop.index-1][0][0]}}_{{friends[loop.index-1][0][1]}}' value="{{number[loop.index -1][0]}}">{{friends[loop.index -1][0][0]}} {{friends[loop.index -1][0][1]}}</a></li>
{% endfor %}
</div>
您是以下方面的朋友:
{范围内x的百分比(朋友|长度)%}
{%endfor%}
您可以通过旅行技术人员在您的问题中的评论来尝试类似的方法。使用.ajax将您的'x'var发送到服务器……之后,您可以使用它执行任何操作
$(document).ready(function(){
$("a").click(function(){
$(this).hide();
let x = $(this).attr('name');
console.log(x);
var url_to_server = "http://localhost/server_response.[php or whatever]?name=x";
jQuery.ajax({
type: "GET",
url: url_to_server,
cache: false,
success: function(response)
{
console.log("sent it to server - update view if needed");
}
});
});
});
问题是什么?您可以执行
$.get(“/friends\u Profile?x=“+x,function(res){console.log(res)})
不确定您的问题是什么:使用$.ajax
(因为您已经在使用jQuery)调用您的服务器路由,将x=name
作为查询字符串传递。我仍然很困惑。。。如何使x的值出现在服务器上的friends_profile()函数中?request.args.get('x')返回None