如何在没有提交按钮的情况下从文本字段调用javascript函数?
我用一些JavaScript代码创建了一个HTML页面。这是密码如何在没有提交按钮的情况下从文本字段调用javascript函数?,javascript,html,dom,Javascript,Html,Dom,我用一些JavaScript代码创建了一个HTML页面。这是密码 <script type="text/javascript"> function doIt(){ window.location = document.getElementById('url').value; } </script> <input type="text" id="url" /> 函数doIt(){ window.locati
<script type="text/javascript">
function doIt(){
window.location = document.getElementById('url').value;
}
</script>
<input type="text" id="url" />
函数doIt(){
window.location=document.getElementById('url')。值;
}
我希望当有人在键入URL后按enter键时。页面将自动重定向到给定的URL。我怎么能做到?请向我推荐一些代码您需要将按键事件处理程序附加到文本框。在事件处理程序中,您将检查按下的键是否为enter。如果是,请执行重定向。查找事件处理程序。您需要将按键事件处理程序附加到文本框。在事件处理程序中,您将检查按下的键是否为enter。如果是,请执行重定向。查找事件处理程序。如果您不使用jQuery,下面是一个完整的示例:
function addEvent(obj, type, fn) {
if (obj.attachEvent) {
obj['e' + type + fn] = fn;
obj[type + fn] = function() {
obj['e' + type + fn](window.event);
}
obj.attachEvent('on' + type, obj[type + fn]);
} else obj.addEventListener(type, fn, false);
}
addEvent(window, 'load', function() {
addEvent(document.getElementById('url'), 'keyup', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13)
window.location.href = this.value;
});
});
示例-
添加事件代码片段的积分:-如果您不使用jQuery,下面是一个完整的示例:
function addEvent(obj, type, fn) {
if (obj.attachEvent) {
obj['e' + type + fn] = fn;
obj[type + fn] = function() {
obj['e' + type + fn](window.event);
}
obj.attachEvent('on' + type, obj[type + fn]);
} else obj.addEventListener(type, fn, false);
}
addEvent(window, 'load', function() {
addEvent(document.getElementById('url'), 'keyup', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13)
window.location.href = this.value;
});
});
示例-
addEvent代码片段的积分:-请参见
见现场演示在
使用以下代码:
<html>
<html>
<head>
<script type="text/javascript">
function doIt(){
window.location.href = document.getElementById('url').value;
}
document.onkeypress=function(e){
var e=window.event || e;
if(e.keyCode===13) doIt();
}
</script>
</head>
<body>
<input type="text" id="url" value="http://www."/>
</body>
</html>
函数doIt(){
window.location.href=document.getElementById('url')。值;
}
document.onkeypress=函数(e){
var e=window.event | e;
如果(e.keyCode==13)doIt();
}
使用以下代码:
<html>
<html>
<head>
<script type="text/javascript">
function doIt(){
window.location.href = document.getElementById('url').value;
}
document.onkeypress=function(e){
var e=window.event || e;
if(e.keyCode===13) doIt();
}
</script>
</head>
<body>
<input type="text" id="url" value="http://www."/>
</body>
</html>
函数doIt(){
window.location.href=document.getElementById('url')。值;
}
document.onkeypress=函数(e){
var e=window.event | e;
如果(e.keyCode==13)doIt();
}
以下是解决方案:
JS
(function () {
var textField = document.getElementById('foo');
function addHandler(el, event, handler) {
if (el.addEventListener) {
el.addEventListener (event, handler, false);
} else if (el.attachEvent) {
el.attachEvent (event, handler);
}
}
addHandler(textField, 'keydown', textEntered);
function textEntered(event) {
if (event.keyCode === 13) {
window.location = textField.value;
}
}
}());
HTML
<input type="text" id="foo" />
致以最良好的祝愿 以下是解决方案:
<input type="text" id="url" onblur="window.location.href='this.value'" />
JS
(function () {
var textField = document.getElementById('foo');
function addHandler(el, event, handler) {
if (el.addEventListener) {
el.addEventListener (event, handler, false);
} else if (el.attachEvent) {
el.attachEvent (event, handler);
}
}
addHandler(textField, 'keydown', textEntered);
function textEntered(event) {
if (event.keyCode === 13) {
window.location = textField.value;
}
}
}());
HTML
<input type="text" id="foo" />
致以最良好的祝愿
<input type="text" id="url" onblur="window.location.href='this.value'" />
以上是如果调焦被取出,如按tab键
然后,对于enter,编写onkeydown处理程序
<script type="text/javascript">
document.getElementById('url').onkeydown = function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
window.location.href = this.value;
}
}
</script>
document.getElementById('url').onkeydown=function(e){
var keyCode=e.keyCode | | e.which;
如果(键代码===13){
window.location.href=this.value;
}
}
以上是如果调焦被取出,如按tab键
然后,对于enter,编写onkeydown处理程序
<script type="text/javascript">
document.getElementById('url').onkeydown = function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
window.location.href = this.value;
}
}
</script>
document.getElementById('url').onkeydown=function(e){
var keyCode=e.keyCode | | e.which;
如果(键代码===13){
window.location.href=this.value;
}
}
您熟悉jQuery吗?您熟悉jQuery吗?我如何在代码中使用此函数?可以发布一个例子吗?@Xander我应该使用jquery文件吗?@RizwanKhan jquery将简化-我已经更新了我的答案以提供一个完整的例子…我如何在我的代码中使用这个函数?可以发布一个例子吗?@Xander我应该使用jquery文件吗?@RizwanKhan jquery将简化-我已经更新了我的答案,提供了一个完整的例子…您将按键
附加到文档
元素
。这不是理想的解决方案…@Xander,你的代码是正确的。但是,我的很容易理解。无论如何,当没有人说你错了时,你可以说你是对的:)你将按键
附加到文档
元素
。这不是理想的解决方案…@Xander,你的代码是正确的。但是,我的很容易理解。不管怎么说,当没有人说你错的时候,你愿意说你是对的:)