简体   繁体   English

JavaScript 使用回车键运行字段搜索

[英]JavaScript using enter to run field search

I'm pretty new to programming and everthing and right now i am trying to improve some internal programs and script.我对编程和任何事情都很陌生,现在我正在尝试改进一些内部程序和脚本。 One of those things is our booking system.其中之一就是我们的预订系统。 Its using JS and field search to search through all the projects but it doesnt work when pressing enter instead of clicking the button.它使用 JS 和字段搜索来搜索所有项目,但是在按 enter 而不是单击按钮时它不起作用。 I tried using a tapermonkey script to fix it but it doesnt catch the input value when pressing enter.我尝试使用tapermonkey脚本来修复它,但在按下回车时它没有捕获输入值。

This is the input and the search button:这是输入和搜索按钮:

<input name="b$s111$l108s111$ctl00$googlesearch$i" 
type="text" 
maxlength="3998" 
id="b_s111_l108s111_ctl00_googlesearch_i" 
class="Edit" 
onfocus="HighlightTextBox(this, 'b$s111$l108s111$ctl00$googlesearch', false, false);" 
datavalidation="false" 
data-sectionid="111" 
data-fieldid="1841" 
onchange="setDirty('b_s111_l108s111_ctl00_googlesearch_IsDirty');
if(!IsLegalInputValue(this,'Search criteria')){return false;}" 
onblur="if(!IsLegalInputValue(this,'Search criteria')){return false;}ResetTextBox(this, 'b$s111$l108s111$ctl00$googlesearch', false, false, 'white');" 
onkeydown="CheckForShiftTab(event);if(event.keyCode == 9 &amp;&amp; !event.ctrlKey){if(!IsLegalInputValue(this,'Search criteria')){return false;}}" 
data-type="String" 
autocomplete="off" 
>

<button id="b_s111__fieldHelpSearch" 
tabindex="10" 
role="button" 
type="button" 
class="BaseButton SectionButton" 
onclick="baseButtonClick(event, 'b$s111$_fieldHelpSearch', true, 'action:_dofieldhelpsearch');" 
onkeydown="this.onclick(event);"
>

And this is the script I wrote:这是我写的脚本:

  var input = document.getElementById("b_s111_l108s111_ctl00_googlesearch_i");
     input.addEventListener("keydown", function(event) {
     if (event.keyCode === 13) {
        document.getElementById("b_s111__fieldHelpSearch").click();
  }
});

Why is the search not working right and how can I fix this?为什么搜索无法正常工作,我该如何解决?

If you want the value inside the input box, then I don't think this line makes sense document.getElementById("b_s111__fieldHelpSearch").click();如果您想要输入框中的值,那么我认为这一行没有意义document.getElementById("b_s111__fieldHelpSearch").click(); Instead you can do:相反,您可以这样做:

const input = document.getElementById('b_s111_l108s111_ctl00_googlesearch_i')
input.addEventListener('keydown',(e)=>{
  if(e.keyCode === 13){
    console.log(e.target.value)
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM