我可以获取 html 中单选按钮的文本以在 Flask 中使用吗?
Am I able to get the text of a radio button in html to use in Flask?
我正在开发一个简单的 Web 应用程序,它可以让用户对电影进行评分和评论,并将它们存储在数据库中以供日后查看。用户输入要评分的电影,如果该电影名称被多部电影共享,系统会提示他们指定他们指的是哪部电影。我已选择在我的 html 文件中使用单选按钮来执行此操作,但我无法弄清楚如何获取所选按钮的文本并在 Flask 中使用它。该应用程序采用所选电影的名称并将该名称打印在另一个 html 文件的顶部,但无论我做什么,它总是打印“None”而不是所选标题。
我找到了这个答案:,这促使我尝试
var confirmed = $("input[name='confirmation']:checked").parent('label').text();
在我的 html 脚本中获取选中按钮的文本。但是当我尝试在 Flask 中请求它时它不起作用。
confirm.html :
<div id="id02" class="modal" data-backdrop="false">
<form class="modal-content animate" action="/confirm" method="post">
<span onclick="document.getElementById('id02').style.display='none'" class="close" title="Close Modal">×</span>
<div class="container">
<h1>Please Specify Which Title to Rate:</h1>
{% for movie in multi %}
<label class="container">{{movie["title"]}} ({{movie["year"]}})
<input type="radio" id={{movie["year"]}} name="confirmation">
<span class="checkmark"></span>
</label>
{% endfor %}
<button type="submit">Rate</button>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</form>
</div>
<script>
// Get the modal when page is loaded
$(window).on('load',function(){
$('#id02').modal('show');
});
var modal = document.getElementById('id02');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
// variable storing the name of the film selected
var confirmed = $("input[name='confirmation']:checked").parent('label').text();
</script>
application.py :
@app.route("/confirm", methods=["GET", "POST"])
def confirm():
if request.method == "POST":
full = request.form.get("confirmed")
session["title"] = full # add movie title to session in order to use in rate function
return render_template("rate.html", full=full)
else:
return render_template("confirm.html")
我也尝试过为按钮分配一个 ID,如果它被选中希望从 ID 中获取文本,但是当我尝试这样做时,我也只得到了“None”。
{% for movie in multi %}
<label class="container">{{movie["title"]}} ({{movie["year"]}})
<input type="radio" name="confirmation">
{% if checked=="checked" %}
<input type="hidden" id="confirmed" value="({{movie["year"]}})">
{% endif %}
<span class="checkmark"></span>
</label>
{% endfor %}
我是编程新手,这是我第一次尝试制作 Web 应用程序,因此非常感谢任何帮助!
当 flask 为您提供单选按钮值时。它为您提供值属性而不是单选按钮文本,因此您只需要将值属性添加到单选按钮
我正在开发一个简单的 Web 应用程序,它可以让用户对电影进行评分和评论,并将它们存储在数据库中以供日后查看。用户输入要评分的电影,如果该电影名称被多部电影共享,系统会提示他们指定他们指的是哪部电影。我已选择在我的 html 文件中使用单选按钮来执行此操作,但我无法弄清楚如何获取所选按钮的文本并在 Flask 中使用它。该应用程序采用所选电影的名称并将该名称打印在另一个 html 文件的顶部,但无论我做什么,它总是打印“None”而不是所选标题。
我找到了这个答案:
var confirmed = $("input[name='confirmation']:checked").parent('label').text();
在我的 html 脚本中获取选中按钮的文本。但是当我尝试在 Flask 中请求它时它不起作用。
confirm.html :
<div id="id02" class="modal" data-backdrop="false">
<form class="modal-content animate" action="/confirm" method="post">
<span onclick="document.getElementById('id02').style.display='none'" class="close" title="Close Modal">×</span>
<div class="container">
<h1>Please Specify Which Title to Rate:</h1>
{% for movie in multi %}
<label class="container">{{movie["title"]}} ({{movie["year"]}})
<input type="radio" id={{movie["year"]}} name="confirmation">
<span class="checkmark"></span>
</label>
{% endfor %}
<button type="submit">Rate</button>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</form>
</div>
<script>
// Get the modal when page is loaded
$(window).on('load',function(){
$('#id02').modal('show');
});
var modal = document.getElementById('id02');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
// variable storing the name of the film selected
var confirmed = $("input[name='confirmation']:checked").parent('label').text();
</script>
application.py :
@app.route("/confirm", methods=["GET", "POST"])
def confirm():
if request.method == "POST":
full = request.form.get("confirmed")
session["title"] = full # add movie title to session in order to use in rate function
return render_template("rate.html", full=full)
else:
return render_template("confirm.html")
我也尝试过为按钮分配一个 ID,如果它被选中希望从 ID 中获取文本,但是当我尝试这样做时,我也只得到了“None”。
{% for movie in multi %}
<label class="container">{{movie["title"]}} ({{movie["year"]}})
<input type="radio" name="confirmation">
{% if checked=="checked" %}
<input type="hidden" id="confirmed" value="({{movie["year"]}})">
{% endif %}
<span class="checkmark"></span>
</label>
{% endfor %}
我是编程新手,这是我第一次尝试制作 Web 应用程序,因此非常感谢任何帮助!
当 flask 为您提供单选按钮值时。它为您提供值属性而不是单选按钮文本,因此您只需要将值属性添加到单选按钮