我在某个div的页面上有一个输入:
<input style='border:1px solid black;' type='text' id='inputFindBy_Name' />
和o jquery javascript函数监视它:
$("div[id=mainGridPage] input").bind("keyup", function (event) {
if (event.keyCode == 13) {
var searchField = "Name";
var searchValue = $(this)[0].value;
var pageIndex = "1";
var sortField = "Name";
Application.Services.ProductTypeService.LoadMainGridProductType(pageIndex, sort, sortField, searchField, searchValue, ResultLoadMainGridProductType, ErrorLoadMainGridProductType);
}
});
当用户输入内容并按下ENTER(event.keyCode == 13
)时,我需要做一些事情但不重新加载页面。怎么样?
答案 0 :(得分:1)
试试这个
$("div[id=mainGridPage] input").bind("keyup", function (event) {
if (event.keyCode == 13) {
// needed do something here without reloading the page
return false;
}
});
就像一个链接。
答案 1 :(得分:0)
您需要event.stopPropagation()
,也许return false;
。请使用event.which
,因为event.keyCode
与所有浏览器都不兼容,您也在使用搜索ID的div[id=mainGridPage] input
,更好的解决方法是:div#mainGridPage input
,并且可能更快。
$("div#mainGridPage input").bind("keyup", function (event) {
if (event.which == 13) {
event.stopPropagation();
// needed do something here without reloading the page
return false.
}
});
答案 2 :(得分:0)
只需从函数中返回false
:
var code = event.keyCode || event.which;
if (code == 13) {
// do what you have to do.....
return false;
}
修改:在已经调度表单提交事件后,“keyup
事件被触发”太晚了 - 您无法在该阶段取消或停止它。因此,请处理keypress
事件。将行更改为:
$("div[id=mainGridPage] input").bind("keypress", function (event) {
return false;
确实会阻止表单提交。
答案 3 :(得分:0)
试试这个。这将是我认为的工作:
$("div[id=mainGridPage] input").keypress(function(event) {
if (event.keyCode == 13) {
// do your code
console.log('hello');
return false;
}
});