[英]How to access and store the input hidden field of a jsp page which is changing dynamically on button clicks?
here is the script that in the jsp page这是 jsp 页面中的脚本
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#trno=${track.track_no}" class="album-poster" data-switch="${count}" data-value="${track.track_no}">Play Button</a>
<input type="hidden" id="trnum" name="trnum" value="">
here is the js script that I wrote这是我写的js脚本
$(".album-poster").on('click', function(e) {
var dataSwitchId = $(this).attr('data-switch');
var datavalue = $(this).attr('data-value');
//Add value to hidden field.
$('#trnum').val(datavalue);
//Show hidden field value
console.log($('#trnum').val())
ap.list.switch(dataSwitchId);
ap.play();
$("#aplayer").addClass('showPlayer');
});
here the anchor tag is in the foreach loop such that of different values will be there in the data-value attribute in the anchor tag这里的锚标签在foreach循环中,这样不同的值就会出现在锚标签的data-value属性中
here I want to store these data values to the database such that when I'm trying to access like在这里,我想将这些数据值存储到数据库中,这样当我尝试访问时
$(".album-poster").on('click', function(e) {
var dataSwitchId = $(this).attr('data-switch');
var datavalue = $(this).attr('data-value');
//Add value to hidden field.
$('#trnum').val(datavalue);
//Show hidden field value
console.log($('#trnum').val())
****<%System.out.println(request.getParameter("trnum"));%>****
ap.list.switch(dataSwitchId);
ap.play();
$("#aplayer").addClass('showPlayer');
});
in the highlighted code that I'm printing in the console it is printing null I need these values which are clicked to store them into the database and how can I get access them and store into the java list.在我在控制台中打印的突出显示的代码中,它正在打印 null 我需要单击这些值以将它们存储到数据库中,以及如何访问它们并将它们存储到 java 列表中。
how can I do it?我该怎么做?
You need to submit your data to server to access request
object.One way to achieve this is using ajax.You can send the datavalue
which is clicked by user to backend and save it.So your jquery code will look like below:您需要将数据提交到服务器以访问
request
object。实现此目的的一种方法是使用datavalue
。您可以将用户单击的数据值发送到后端并保存。因此您的ZD223E1439188E478349EZ代码如下所示:
$(".album-poster").on('click', function(e) {
var dataSwitchId = $(this).attr('data-switch');
var datavalue = $(this).attr('data-value');
//chcking if datavalue is not null
if (datavalue != null) {
$.ajax({
url: "your_backend_url", //url or servlet
type: "GET",
data: {
trnum: datavalue
}, //pass data to backend access same using request.getParameter("trnum") in doGet method servlet
success: function(data) {
console.log("done");//this will print on success in browser console
}
});
}
ap.list.switch(dataSwitchId);
ap.play();
$("#aplayer").addClass('showPlayer');
});
At backend get that data which is passed by user using request.getParameter("trnum")
and do further operations.在后端获取用户使用
request.getParameter("trnum")
传递的数据并执行进一步操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.