javascript - 将文本从文本框传递到 jsp 中的 href 调用的按钮
问题描述
我试图让用户单击一个按钮,然后该按钮在文本框中获取值并将其添加到 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 和 value 之间添加斜杠 在以下代码段中,newHref
变量是
location.href
& 文本框值的串联。如果您打算导航到设置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>
推荐阅读
- excel - 如何在 VBA 中的 Evaluate 函数中添加分隔符?
- jsf - 如何在我的代码中实现 primefaces 延迟加载
- java-8 - 从地图中获取值列表
- reporting-services - 如何循环记录,以便每行有 2 个记录,一个在左侧,另一个在右侧,这可能吗?
- python - 如何通过 setup.py 安装 Python 包,该包依赖于可能无法在用户计算机上安装的 C 二进制文件
- java - android通过图库意图选择的相机图像未显示在imageview中
- c# - 如何将文本框的文本双向数据绑定到依赖属性
- asp.net-mvc - 我应该如何解释这个 MiniProfiler 结果?
- apache-kafka - 如何使用 KTable 从 kafka 1.1.0 获取最新值
- javascript - 如何将 Alert 替换为 navigation.navigate('Home")? like Open the new screen in catch