英文:
Call a Java SpringBoot server method from a JavaScript script on a website
问题
<ul class="collapse list-unstyled" id="selectGameSubmenu">
<li data-option="League of Legends">
<a href="#"><img src="../../../static/images/league_of_legends/leagueoflegends_icon.png"
alt="" style="width:16px;height:16px"> League of Legends</a>
</li>
<li data-option="Teamfight Tactics">
<a href="#"><img src="../../../static/images/teamfight_tactics/teamfighttactics_icon.png"
alt="" style="width:16px;height:16px"> Teamfight Tactics</a>
</li>
<li data-option="Legends of Runterra">
<a href="#"><img src="../../../static/images/legends_of_runterra/legendsofruneterra_icon.png"
alt="" style="width:16px;height:16px"> Legends of Runterra</a>
</li>
<li data-option="Valorant">
<a href="#"><img src="../../../static/images/valorant/valorant_icon.png"
alt="" style="width:16px;height:16px"> Valorant</a>
</li>
<li data-option="Counter Strike">
<a href="#"><img src="../../../static/images/counter_strike/counterstrike_icon.png"
alt="" style="width:16px;height:16px"> Counter Strike</a>
</li>
</ul>
$("#selectGameSubmenu").click(function(e){
e.preventDefault();
var option = $("#selectGameSubmenu li").data("option");
console.log(option);
});
@PostMapping("/change-game")
public String changeGame(@RequestParam String game){
System.out.println("Selected option: " + game);
return "index";
}
英文:
I'm working on a submenu on my website, that has 5 options, each of them is name of a game. When user selects an option, I want my server to print a name of the game, that user selected. Here i have two problems:<br>
- I don't know why after click on a submenu option, I always get the
first game name. <br> - I don't know how to send a request from a JS to my Spring Boot application, which is a server. <br>
Can you help me? Here is my code:
<ul class="collapse list-unstyled" id="selectGameSubmenu">
<li data-option="League of Legends">
<a href="#"><img src="../../../static/images/league_of_legends/leagueoflegends_icon.png"
alt="" style="width:16px;height:16px"> League of Legends</a>
</li>
<li data-option="Teamfight Tactics">
<a href="#"><img src="../../../static/images/teamfight_tactics/teamfighttactics_icon.png"
alt="" style="width:16px;height:16px"> Teamfight Tactics</a>
</li>
<li data-option="Legends of Runterra">
<a href="#"><img src="../../../static/images/legends_of_runterra/legendsofruneterra_icon.png"
alt="" style="width:16px;height:16px"> Leagends of Runterra</a>
</li>
<li data-option="Valorant">
<a href="#"><img src="../../../static/images/valorant/valorant_icon.png"
alt="" style="width:16px;height:16px"> Valorant</a>
</li>
<li data-option="Counter Strike">
<a href="#"><img src="../../../static/images/counter_strike/counterstrike_icon.png"
alt="" style="width:16px;height:16px"> Counter Strike</a>
</li>
</ul>
$("#selectGameSubmenu").click(function(e){
e.preventDefault();
var option = $("#selectGameSubmenu li").data("option");
console.log(option);
});
@PostMapping("/change-game")
public String changeGame(@RequestParam String game){
System.out.println("Selected option: " + game);
return "index";
}
答案1
得分: 1
你的第一个问题出现在你的点击事件监听器下。所以每当你点击你的ul元素时,它都会触发,但是由于你定义了这样一个选择器 $("#selectGameSubmenu li")
,它总是会返回你的ul
的第一个子元素,也就是League of Legends
。
要解决这个问题,你应该稍微修改一下。首先,不要在你的ul
上添加事件监听器,而是在你的li
上添加事件监听器,然后通过使用$(this)
获取特定的点击项,输出应该像这样:
$("li").click(function (e) {
e.preventDefault();
var option = $(this).data('option');
console.log(option);
});
你的第二个问题很简单,要在你的后端和前端应用程序之间进行通信,你应该使用类似于ajax
的东西。ajax
请求将有助于在应用程序之间检索和发送数据。关于它的更多信息,你可以阅读以下这些文章之一:JQuery文档,Site point,或者Free code camp。
你的请求应该像这样:
$.ajax({
type: "POST",
url: url, // 你的终端地址
data: data, // 你的数据
success: success // 在数据成功发送后执行的操作
});
英文:
Your first problem lies under your click event listener. So whenever you click on your ul elements it will trigger but as you define such a selector $("#selectGameSubmenu li")
it will always return your first child of ul
which is League of Legends
.
To fix it you should modify it a bit. First of all, instead of adding event listener in your ul
you should add an event listener in your li
and then get the specific clicked item by using $(this)
, the output should be something like this:
$("li").click(function (e) {
e.preventDefault();
var option = $(this).data('option');
console.log(option);
});
Your second problem is simple, for communicating between your backend and frontend application you should use something like ajax
. An ajax
request will help to retrieve and send data between applications. For more information about it, you can read either of these articles: JQuery documentation, Site point, or Free code camp.
Your request will be something like this:
$.ajax({
type: "POST",
url: url, // Your end point address
data: data, // Your data
success: success // Your actions after data send successfully
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论