简体   繁体   English

如何访问和存储在按钮单击时动态变化的 jsp 页面的输入隐藏字段?

[英]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.

相关问题 如何访问从JSP传递的参数作为HTML页面的隐藏输入? - How to access parameters passed from JSP as hidden input to HTML page? 当用户单击按钮时,我动态呈现日期输入字段 html5 但无法访问事件侦听器,例如更改 - When a user clicks a button, I dynamically render date input field html5 but can't access event listener such as change 如何从通过 Javascript function 动态创建的输入字段访问表单输入? - How can I access form input from an input field which was created dynamically via a Javascript function? 如何将onchange选择的下拉值存储到隐藏的输入字段中? - How to store onchange selected dropdown value into hidden input field? 如何动态删除使用JavaScript动态创建的输入字段 - How to dynamically remove input field which is created dynamically using JavaScript 如何在按钮点击时获取最近的隐藏输入字段的值 - How to get value of closest hidden input field on button click 动态隐藏的输入字段以后无法显示 - Input field hidden dynamically can not be shown later "在输入隐藏字段中存储返回 json 值" - store return json value in input hidden field 在隐藏字段中存储和访问值的最简单方法 - Easiest way to store and access values in hidden field 将模型作为JSON存储在隐藏的输入中,并在javascript中访问 - Store Model as JSON in Hidden input and access it in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM