jQuery function execute on Button Click and Enter/Return (key)

后端 未结 3 1728
盖世英雄少女心
盖世英雄少女心 2021-02-08 23:54

I\'m trying to create a little search box that allows you to search Twitter based on the keyword you enter in the input field. While it\'s work, it only works if you press the S

3条回答
  •  悲哀的现实
    2021-02-09 00:36

    Try create listeners for both key press and mouse clicks.

    function myFunction(e){
        if (e.which=='13' || e.type=='click'){
            // Run your code here
        }
    }
    $(document)
      .on('click', '#startSearch', myFunction)
      .on('keypress', this, myFunction);
    

    In action http://codepen.io/scottyzen/pen/akWPJd

提交回复
热议问题