简体   繁体   English

JSP - 在同一页面上将值传递给模态

[英]JSP - Passing values to modal on the same page

I have a jsp page that contains a username and a button, When I click on the button I want to pass the username to the modal then in the click on the modal button I send the username to the back-end (struts2 action).我有一个包含用户名和按钮的 jsp 页面,当我单击按钮时,我想将用户名传递给模态,然后在单击模态按钮时将用户名发送到后端(struts2 操作)。 (Scenario illustrated in the below picture) (场景如下图所示)

I tried to pass username with onClick:我尝试使用 onClick 传递用户名:

<%-- Page.jsp --%>
<button onclick="document.forms[0].username.value='BOB'; toggle="modal" data-target="#myModal">
   <fmt:message key='Delete user'/>
</button>
<jsp:include page="deleteUserModal.jsp"/>

On the modal I want to read unsername value:在模态我想读取 unsername 值:

<%-- Modal.jsp --%>
<button onclick="document.forms[0].username.value='???';document.forms[0].action='deleteMethod';document.forms[0].submit();">
  <fmt:message key='delete'/>
</button>

Any idea please,请有任何想法,

在此处输入图像描述

You trying to access elements from the another window.您试图从另一个 window 访问元素。 To customize the modal content you should use HTML data-* attributes.要自定义模态内容,您应该使用 HTML data-*属性。 See how to use verying modal content with bootstrap.了解如何通过引导程序使用非常模态内容

<button onclick="document.forms[0].username.value='BOB'; toggle="modal" data-target="#myModal" data-username='BOB'>
   <fmt:message key='Delete user'/>
</button>
$('#myModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) 
  var username = button.data('username') 
  var modal = $(this)
  modal.find('.modal-body button').click(function(){
    alert(username);
})
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM