Zendesk Guide Search On Enter Key Disable Function

huangapple go评论123阅读模式
英文:

Zendesk Guide Search On Enter Key Disable Function

问题

I'll provide translations for the code parts you've shared:

// Submit search on input enter
$quickSearch.on('keypress', (e) => {
    if (e.which === 13) {
        search();
    }
});
const searchBox = document.querySelector("query");
searchBox.addEventListener("keydown", (event) => {
    if (event.keyCode === 13) {
        event.preventDefault();
    }
});
$(document).ready(function() {
    $(window).keydown(function(event){
        if(event.keyCode == 13) {
            event.preventDefault();
            return false;
        }
    });
});

Is there anything else I can assist you with?

英文:

I have been trying to disable the search on enter key press function within Zendesk Guide but nothing seems to take effect in my theme scripts, any suggestions?

1.
 
  // Submit search on input enter
  $quickSearch.on('keypress', (e) => {
    if (e.which === 13) {
      search();
    }
  });
});
 
 
2.
 
const searchBox = document.querySelector("query"); searchBox.addEventListener("keydown", (event) => { if (event.keyCode === 13) {   event.preventDefault(); }});*/
 
 
3.
 
  $( document ).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });

答案1

得分: 0

I will provide the translation without the code part:

你需要确保你的事件监听器在任何其他事件监听器之前调用。尝试使用全局事件监听器,并将addEventListener()useCapture参数设置为true,然后调用stopImmediatePropagation()方法。

类似这样的代码应该起作用:

$(document).ready(function() {
  document.addEventListener('keydown', (e) => {
    if (e.which == 13 && e.target.id == "query") {
      e.preventDefault()
      e.stopImmediatePropagation()
      // 进行一些操作
    }
  }, true)
})

If you need further assistance or have more specific questions, feel free to ask.

英文:

You need to make sure your event listener is called before any other event listener. Try to use a global event listener and set the useCapture parameter of addEventListener() to true, then call the stopImmediatePropagation() method.

Something like this should work:

 $(document).ready(function() {
  document.addEventListener('keydown', (e)=>{
    if (e.which == 13 && e.target.id == "query") {
      e.preventDefault()
      e.stopImmediatePropagation()
      //do something
    }
  }, true)
})

huangapple
  • 本文由 发表于 2023年4月6日 21:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定