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

javascript - How to trigger a function on Enter key - Stack Overflow

programmeradmin0浏览0评论

I would like to add code that allows me to use the enter key in addition to a cursor click to initialize a google map at the location that is submitted via a text box. I have the click part down, the enter key, not so much :(

    <input id="address" type="text">
    <input id="search" type="button" value="search" onClick="search_func()">

    <script>
      function search_func() {
        var address = document.getElementById("address").value;
        initialize();
      }
    </script>

I would like to add code that allows me to use the enter key in addition to a cursor click to initialize a google map at the location that is submitted via a text box. I have the click part down, the enter key, not so much :(

    <input id="address" type="text">
    <input id="search" type="button" value="search" onClick="search_func()">

    <script>
      function search_func() {
        var address = document.getElementById("address").value;
        initialize();
      }
    </script>
Share Improve this question edited Dec 15, 2022 at 13:35 rob2universe 7,62843 silver badges59 bronze badges asked Aug 16, 2015 at 5:15 Bar Bar 311 gold badge1 silver badge3 bronze badges 1
  • Can you provide the rest of your code? You didn't give very much. Also, make sure to properly format your code blocks by putting four spaces before each line of code. – Tim Commented Aug 16, 2015 at 5:19
Add a ment  | 

3 Answers 3

Reset to default 3

Here is your solution:

<!DOCTYPE html>

<html>

<head>
<title>WisdmLabs</title>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<style>

</style>

</head>

<body>

<input id="address" type="text" onkeypress="handle(event)" placeholder="Type something here">
<input id="search" type="button" value="search" onClick="search_func()">

<script>

function search_func(){
	address=document.getElementById("address").value;
	//write your specific code from here	
	alert("You are searching: " + address);
}

function handle(e){
	address=document.getElementById("address").value;
    if(e.keyCode === 13){
		//write your specific code from here
    	alert("You are searching: " + address);
    }
	return false;
}

</script>


</body>

</html>

Feel free to ask any doubts or suggestions.

You would want to add a listener to your text box that fires search_func on keydown, when the key pressed is enter (13 is the keyCode for Enter):

<input id="address" type="text" onkeydown="key_down()">
<input id="search" type="button" value="search" onClick="search_func()">

<script>
  function key_down(e) {
    if(e.keyCode === 13) {
      search_func();
    }
  }

  function search_func() {
    var address = document.getElementById("address").value;
    initialize();
  }
</script>
function search_func(e)
{
    e = e || window.event;
    if (e.keyCode == 13)
    {
        document.getElementById('search').click();
        return false;
    }
    return true;
}
发布评论

评论列表(0)

  1. 暂无评论