Javascript 如何使文本输入字段根据用户输入打开不同的链接
我无法想象这会很难,但我一直在努力找出最好的方法,老实说,我完全迷路了。现在我感到很沮丧,所以如果有人知道如何做到这一点,能帮我解决问题就太好了 我需要一个文本框,我可以嵌入到我的html脚本,这将打开不同的链接取决于用户输入的文本 因此,在英语这里是我需要的基本知识。一份工作脚本将非常感谢,因为我被难倒了Javascript 如何使文本输入字段根据用户输入打开不同的链接,javascript,html,forms,redirect,input,Javascript,Html,Forms,Redirect,Input,我无法想象这会很难,但我一直在努力找出最好的方法,老实说,我完全迷路了。现在我感到很沮丧,所以如果有人知道如何做到这一点,能帮我解决问题就太好了 我需要一个文本框,我可以嵌入到我的html脚本,这将打开不同的链接取决于用户输入的文本 因此,在英语这里是我需要的基本知识。一份工作脚本将非常感谢,因为我被难倒了 * stands for “any characters” If text input contains bbb open link htttp://example1.com/
* stands for “any characters”
If text input contains bbb open link htttp://example1.com/bbb
otherwise
If text input contains *zzz* open link htttp://example2.com/something-predefined*zzz*
otherwise
If text input contains *xxx* open link htttp://example3.com/something-predefined*xxx*
otherwise
If text input contains * open link htttp://example3.com/*
伪代码:
function parmatch(input):
if input matches /^bbb$/
open 'http://example1.com/{input}'
return
if input matches /zzz/
open 'http://example2.com/something-predefined{input}'
return
if input matches /xxx/
open 'http://example3.com/something-predefined{input}'
return
以下代码只是您所问问题的示例。这里是控制台的更改。
$('#picker').keyup(函数(){
var值=$(“#选取器”).val();
如果(值=“1”)
{
控制台日志(“回家”);
}
否则如果(值=“2”)
{
log(“转到登录”);
}
其他的
警报(“plz输入有效输入”);
})
要获得更完整的答案,我建议如下:
var btn = document.getElementById('fire'),
input = document.getElementById('demo'),
output = document.getElementById('output'),
pageMap = {
'aaa' : 'pageA',
'bbb' : 'pageB',
'ccc' : 'pageC'
};
function loadPage (input, output, map) {
var v = input.value,
url = 'http://example.com/',
text = 'textContent' in document ? 'textContent' : 'innerText';
for (var i in map) {
if (map.hasOwnProperty(i) && v.indexOf(i) > -1) {
// the following shows the URL you'd end up with
output[text] = url + map[i];
// to *load* the URL, remove the above line,
// and uncomment the following:
// window.location = url + map[i];
}
}
}
btn.addEventListener('click', function(e){
e.preventDefault();
loadPage(input, output, pageMap);
});
然而,这个答案并不是为了鼓励您避免学习JavaScript(如对的评论中所建议的);请花点时间阅读,至少,我在下面提供的参考资料
参考资料:
type=“text”
的输入。没有“文本框”这样的元素,显然有一个textarea
,但那仍然不是“文本框”。@DavidThomas我来自ASP.Net时代,认为textbox==对不起,如果我弄错了,我知道区别。无论如何谢谢:)哈哈。非常感谢。我一直在试图通过你已经链接到我的页面来解决这个问题。还是。如何将其链接到文本框?我想这里会对此进行解释。。。幸运的是,你不需要了解Java,只需要了解JavaScript。我真的不明白这是多么有用。你能告诉我我需要做什么才能正确地将它放入body标记中吗?
var btn = document.getElementById('fire'),
input = document.getElementById('demo'),
output = document.getElementById('output'),
pageMap = {
'aaa' : 'pageA',
'bbb' : 'pageB',
'ccc' : 'pageC'
};
function loadPage (input, output, map) {
var v = input.value,
url = 'http://example.com/',
text = 'textContent' in document ? 'textContent' : 'innerText';
for (var i in map) {
if (map.hasOwnProperty(i) && v.indexOf(i) > -1) {
// the following shows the URL you'd end up with
output[text] = url + map[i];
// to *load* the URL, remove the above line,
// and uncomment the following:
// window.location = url + map[i];
}
}
}
btn.addEventListener('click', function(e){
e.preventDefault();
loadPage(input, output, pageMap);
});