如何从输入字段中获取值并将其值插入到另一个输入中?

How can I get the value from input field and insert its value into another input?

我在将任何输入值插入另一个输入值时遇到问题。

这些来自selvlet的输入值定义如下。这些值是正确的。我无法在表格中包含它们。

<input type="text" class="form-control" id="recipientNameText" name="recipientName" value="${bookOrder.recipentName}"  size="45" />
<input type="text" class="form-control" id="recipientPhoneText" name="recipientPhone" value="${bookOrder.recipentPhone}"  size="45" />
<input type="text" class="form-control" id="shippingAddressText" name="shippingAddress" value="${bookOrder.shippingAddress}"  size="45" />

我的表格在这里

<form action="save_book_order" action="GET" class="form-inline">
  <input id="orderId" name="orderId" type=hidden value="${bookOrder.id}">
  <input type="hidden" class="form-control" id="recipientNameHidden" name="recipientName" onchange="test1()"  size="45" />
  <input type="hidden" class="form-control" id="recipientPhoneHidden" name="recipientPhone" onchange="test2()" size="45" />
  <input type="hidden" class="form-control" id="shippingAddressHidden" name="shippingAddress" onchange="test3()" size="45" />   
</form>

这些输入值是指表格上方显示的输入值。

我想从 recipientNameTextrecipientPhoneTextshippingAddressText 获取值并将它们放入这些值中,但我无法处理。 我该如何解决?

这是我的 javascript 代码。

function test1(){
  var recipientNameValue = document.getElementById('#recipientNameText').val();
  $('#recipientNameHidden').val();
}

function test2(){
  var recipientNameValue = document.getElementById('#recipientPhoneText').val();
  $('#recipientPhoneHidden').val();
}

function test3(){
  var recipientNameValue = document.getElementById('#shippingAddressText').val();
  $('#shippingAddressHidden').val();
}

当我从输入字段获取值并在 servlet 中检查它们时,这些都是空的。

String recipientName = request.getParameter("recipientName");
        String recipientPhone = request.getParameter("recipientPhone");
        String shippingAddress = request.getParameter("shippingAddress");

        System.out.println("SaveBookOrderServlet | recipientName : " + recipientName);
        System.out.println("SaveBookOrderServlet | recipientPhone : " + recipientPhone);
        System.out.println("SaveBookOrderServlet | shippingAddress : " + shippingAddress);

输出:

SaveBookOrderServlet | recipientName : 
SaveBookOrderServlet | recipientPhone : 
SaveBookOrderServlet | shippingAddress : 

我不明白你想要什么,但你的代码有些错误。 像这样获取具有指定 ID 的元素:

document.getElementById('recipientNameText');

无需将 # 添加到选择器中。

使用javascript获取输入值:

document.getElementById('recipientNameText').value;

使用javascript设置输入值:

document.getElementById('recipientNameText').value = "some value";
   Please use oncick function on the text type field, not on the hidden field like this - 

 <input type="text" class="form-control" id="recipientNameText" name="recipientName"  size="45" onchange="test1()" />

and use javascript code like this -

        function test1(){
              var recipientNameValue = document.getElementById('recipientNameText').value;
              document.getElementById('recipientNameHidden').value = recipientNameValue;
            }