将文本从文本框传递到 jsp 中的 href 调用的按钮

Button to pass text from a text box into href call in jsp

我正在尝试让用户单击一个按钮,该按钮然后获取文本框中的值并将其添加到 href 调用的末尾。我目前有一个将两个字符串加在一起并调用 href 的函数,但是当单击按钮时,似乎什么也没有发生。这是我到目前为止得到的:

   <!DOCTYPE html>
<html>
  <head>
    <script>

function findAccountID() {
  var text = document.getElementById('textInput');
  var value = encodeURIComponent(text.value); //encode special characters
  location.href = '**href path here**'+value; //goto URL
}

</script>
  </head>
  <body>
    <div>
      <input type="text" id="textInput" />
      <input type="button" value="Find Account ID" onclick="findAccountID();" />
    </div>
  </body>
</html>

您可能需要在 location.href 和值之间添加斜线 / 在下面的代码片段中,newHref 变量是 location.href & 文本框值。如果您打算导航到新 url,请将 location.href 设为 newHref

function findAccountID() {
  var text = document.getElementById('textInput');
  var value = encodeURIComponent(text.value); //encode special characters
  let newHref = location.href + '/' + value
  console.log(newHref)
}
<div>
  <input type="text" id="textInput" />
  <input type="button" value="Find Account ID" onclick="findAccountID();" />
</div>