[英]Why submit input by clicking the enter button not work on mobile phone browsers?
I got a strange problem.我遇到了一个奇怪的问题。 I want to search for a product by clicking enter button.我想通过单击输入按钮来搜索产品。 I used e.which == 13 || e.which == 10
我用e.which == 13 || e.which == 10
e.which == 13 || e.which == 10
with keypress keyup
trigger, like below. e.which == 13 || e.which == 10
与keypress keyup
触发器,如下所示。
<form id="fform" method="get" autocomplete="off" action="/search">
<div class="search-inner-container">
<button @if(isListing) { id="buttonSearchTextListing" } aria-label="unf-search-btn" type="submit"
class="search-button search-keyword" value="Search"></button>
<input
style="cursor: text;"
@if(isListing) {
form='fform'
onclick="!this.form && document.getElementById('fform').submit()"
name="srp-searchText"
} else {
name="srp-searchText"
}
id="searchbar-top"
autocomplete="off"
type="searchText" class="searchbar-text search-input bt1 @uuid" aria-label="Search"
value="@text" placeholder='@l("searchBar.buttonLabel")'
hx-get="/search-autocomplete"
hx-vars="'srp-userInput': getSearchbarVal()"
hx-trigger="keyup changed delay:500ms"
hx-target=".searchbar-suggestion-container"/>
<!-- hx-trigger="keyup changed delay:500ms, focus changed delay:500ms" -->
</div>
</form>
$('.searchbar-text').each(function(){
$(this).on("keyup keypress", function(e) {
if(e.which == 13 || e.which == 10) {
// code
}
});
})
I've tried this on my laptop browser (chrome & Mozilla Firefox).我已经在我的笔记本电脑浏览器(chrome 和 Mozilla Firefox)上试过这个。 I tried with the element inspector on the desktop/mobile display, it works fine.我尝试在桌面/移动显示器上使用元素检查器,它工作正常。 But when I try it on a mobile phone browser, it doesn't work.但是当我在手机浏览器上尝试时,它不起作用。 When I press enter, it jumps to another section and not submit the form.当我按下回车键时,它会跳转到另一个部分并且不提交表单。 This only happens on the product search page on mobile phones but on other pages, it works fine.这只发生在手机的产品搜索页面上,但在其他页面上,它工作正常。
*I tried to debug the clicked keyCode on alert, when on the homepage it appears the keyCode is 13, but when in product search the alert keycode doesn't appear *我尝试在警报上调试单击的keyCode,在主页上显示keyCode为13,但在产品搜索中没有出现警报keycode
if you want to try, just test from website that i've develop HERE , and try in the desktop - mobile phone browser to search (eg saw) and click enter如果您想尝试,只需从我开发的网站测试HERE ,然后尝试在桌面 - 手机浏览器中搜索(例如锯)并单击输入
Can anyone explain why this happens?谁能解释为什么会这样?
Simply set type='submit'
to the button and set display none.只需将type='submit'
设置为按钮并设置 display none。 Make sure to wrap the input field and the button in form tags.确保将输入字段和按钮包装在表单标签中。 Then handle the submit.然后处理提交。
<form>
<input type='text' placeholder='Search for an item'>
<button type='submit'>Search</button>
</form>
First of all,首先,
using UIEvent.which
seems to be a "non-standard."使用UIEvent.which
似乎是“非标准”。 Use KeyboardEvent.keyCode
instead.请改用KeyboardEvent.keyCode
。
Also, the reason "type=submit" didn't work is because you used it on a <button>
element.此外,“type=submit”不起作用的原因是您在<button>
元素上使用了它。 They are not technically "input's," so you can use type
on it.从技术上讲,它们不是“输入”,因此您可以在其上使用type
。
If you try using an <input>
element, it should work perfectly fine.如果您尝试使用<input>
元素,它应该可以正常工作。 Something like this should work:像这样的东西应该工作:
<input aria-label="unf-search-btn" type="submit" class="search-button search-keyword" value="Search"/>
Then assuming you have the <form>
element set up, it should work fine.然后假设您设置了<form>
元素,它应该可以正常工作。
Your code is working exactly you want in my mobile, try putting return false;
您的代码在我的手机中完全符合您的要求,请尝试将return false;
on input submit在输入提交
$('.searchbar-text').each(function(){
$(this).on("keyup keypress", function(e) {
if(e.which == 13 || e.which == 10) {
console.log('Submitted');
}else{
console.log(e.which);
}
return false;
});
});
You likely need to add e.preventDefault()
as well.您可能还需要添加e.preventDefault()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.