我有一个搜索字段:
<div class="search-fill">
<input type="search" name="search" id="search" class="autocomplete" autocomplete="off" placeholder="Search..." value="@ViewBag.Terms"/>
</div>
和与此相关的按钮:
<div class="search-submit">
<button id="searchbutton" class="search-button"><i class="fa fa-search" onclick="removeSpecialChar()"></i></button>
当用户按下 Enter 键时,此代码无济于事。我知道我们可以通过preventDefault()禁用Enter,但是还有其他方法可以做到这一点,因为我希望用户即使单击Enter也可以进行搜索?
答案 0 :(得分:0)
检查
_/read
$(function () {
$('[type="search"]').bind('paste input', removeSpecialChars);
})
function removeSpecialChars(e) {
var self = $(this);
setTimeout(function () {
var initVal = self.val(),
outputVal = initVal.replace(/[^A-Z0-9]+/i, '');
if (initVal != outputVal) self.val(outputVal);
});
}
答案 1 :(得分:0)
document.querySelector("#search").addEventListener("input", ({ target }) => {
const regEx = /([.*+?^$|(){}\[\]])/mg; // example, compose regexp you need
target.value = target.value.replace(regEx, "");
})