最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How do i call a javascript function from a text field without submit button? - Stack Overflow

programmeradmin1浏览0评论

I have create a HTML page with some JavaScript code. Here is the code

   <script type="text/javascript">
     function doIt(){
        window.location = document.getElementById('url').value;
    }
   </script>
   <input type="text" id="url" />

I want that when some one press the enter after type the URL. The page redirect to the given URL automatically. How i can do this? Please suggest me some code

I have create a HTML page with some JavaScript code. Here is the code

   <script type="text/javascript">
     function doIt(){
        window.location = document.getElementById('url').value;
    }
   </script>
   <input type="text" id="url" />

I want that when some one press the enter after type the URL. The page redirect to the given URL automatically. How i can do this? Please suggest me some code

Share Improve this question asked Jan 5, 2012 at 18:02 Rizwan KhanRizwan Khan 4013 gold badges10 silver badges17 bronze badges 1
  • are you familiar with jQuery? – c0deNinja Commented Jan 5, 2012 at 18:06
Add a ment  | 

6 Answers 6

Reset to default 1

Use below code:

<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>

see live demo at http://jsfiddle/qiao/RGqwD/2/

var input = document.getElementById('url');
input.onkeydown = function(e) {
  var key = e.keyCode || e.which;
    if (key == 13) {
      window.location = input.value;
    }
};

Here is solution: http://jsfiddle/YBLQx/1/

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" />

Best regards!

<input type="text" id="url" onblur="window.location.href='this.value'" />

the above is if the focus is taken out, like pressing tab

and then for enter, write onkeydown handler

<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>

If your not using jQuery, the following is a plete example:

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;
    });
});

Example - http://jsfiddle/4PDt4/

Credits for the addEvent code snippet: - http://ejohn/projects/flexible-javascript-events/

You need to attach a keypress event handler to the text box. In your event handler, you would check if the key pressed was enter. If so, do the redirect. Look up event handlers.

发布评论

评论列表(0)

  1. 暂无评论