如何将 scala 模板中的 ID 放入 AJAX 调用中?
How to put an ID from a scala template into an AJAX call?
我正在使用 play framework 2.3.8 并构建了一个应用程序,您可以在其中输入问题并回答问题。
我的视图 class 得到一个 List[Question]
我 运行 通过每个循环并显示它们:
@for(question <- questionList){
<!-- Questions -->
<li class="list-group-item" >
<button type="button" class="btn btn-default" id="upvoteButton"
value="voteUp" style="width: 30px; height: 30px; text-align: center; vertical-align: center;">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true" style="color:orange"></span>
</button>
<span class="num"> @question.voteScore </span>
(...)
这是它的样子:
通过 jQuery 您可以对 QA 投赞成票/反对票,现在我想使用 AJAX 将更改后的 voteScore 放入数据库中。单击按钮会启动一个 AJAX POST,它会向我的控制器发送当前分数,稍后还应发送问题 ID:
$(document).ready(function(){
$(".btn-default").click(function(){
if( $(this).val() == 'voteUp'){
$(this).html( "<span class='glyphicon glyphicon-arrow-up' aria-hidden='true' style='color:orange'></span>" );
var currentScore = $(this).parent('li').find('.num').text();
$(this).parent('li').find('.num').html(parseInt(currentScore) + 1);
$.ajax({
type : 'POST',
url : '@routes.Application.voteUp()',
data : {
text : currentScore
},
success : function(data){
}
});
});
});
但是我不知道如何把questionID放到AJAXPOST,id : @question.questionID
在AJAXPOST的数据部分没有工作(找不到值错误)。我该怎么做?
首先提供存储问题ID的属性:
<span class="num" id="@question.questionID"> @question.voteScore </span>
其次提供 JS 变量来存储所选问题的 ID:
var currentId = $(this).parent('li').find('.num').attr('id');
第三次通过Ajax将其作为JSON发送给控制器:
$.ajax({
type : 'POST',
url : '@routes.Application.voteUp()',
data : {
score : currentScore, id_question : currentId
},
success : function(data){
}
});
});
最后,从 Controller 获取分数和 id_question。
我正在使用 play framework 2.3.8 并构建了一个应用程序,您可以在其中输入问题并回答问题。
我的视图 class 得到一个 List[Question]
我 运行 通过每个循环并显示它们:
@for(question <- questionList){
<!-- Questions -->
<li class="list-group-item" >
<button type="button" class="btn btn-default" id="upvoteButton"
value="voteUp" style="width: 30px; height: 30px; text-align: center; vertical-align: center;">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true" style="color:orange"></span>
</button>
<span class="num"> @question.voteScore </span>
(...)
这是它的样子:
通过 jQuery 您可以对 QA 投赞成票/反对票,现在我想使用 AJAX 将更改后的 voteScore 放入数据库中。单击按钮会启动一个 AJAX POST,它会向我的控制器发送当前分数,稍后还应发送问题 ID:
$(document).ready(function(){
$(".btn-default").click(function(){
if( $(this).val() == 'voteUp'){
$(this).html( "<span class='glyphicon glyphicon-arrow-up' aria-hidden='true' style='color:orange'></span>" );
var currentScore = $(this).parent('li').find('.num').text();
$(this).parent('li').find('.num').html(parseInt(currentScore) + 1);
$.ajax({
type : 'POST',
url : '@routes.Application.voteUp()',
data : {
text : currentScore
},
success : function(data){
}
});
});
});
但是我不知道如何把questionID放到AJAXPOST,id : @question.questionID
在AJAXPOST的数据部分没有工作(找不到值错误)。我该怎么做?
首先提供存储问题ID的属性:
<span class="num" id="@question.questionID"> @question.voteScore </span>
其次提供 JS 变量来存储所选问题的 ID:
var currentId = $(this).parent('li').find('.num').attr('id');
第三次通过Ajax将其作为JSON发送给控制器:
$.ajax({
type : 'POST',
url : '@routes.Application.voteUp()',
data : {
score : currentScore, id_question : currentId
},
success : function(data){
}
});
});
最后,从 Controller 获取分数和 id_question。