【问题标题】:Button to pass text from a text box into href call in jsp将文本从文本框传递到 jsp 中的 href 调用的按钮
【发布时间】:2018-11-15 05:50:20
【问题描述】:

我试图让用户单击一个按钮,然后该按钮将文本框中的值添加到 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>

【问题讨论】:

    标签: javascript html href


    【解决方案1】:

    您可能需要在 location.href 和 value 之间添加斜线 / 在下面的 sn-p 中,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>

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 2021-09-11
      • 2014-11-29
      • 2011-10-07
      • 2014-01-02
      • 2021-07-18
      • 2021-01-27
      • 1970-01-01
      • 2017-07-13
      相关资源
      最近更新 更多