在我的 HTML 页面中,我有一个文本字段,我在其中显示一个提示,并为其分配一个文本更改事件(在用户停止输入 1 秒后起作用)。
但我不能让他们一起正常工作。
我希望该RunSearch
功能在输入字段中的文本更改时发生。但是当文本为空字符串或提示字符串时,它需要忽略。但是,当我关注该字段时,文本从提示变为空字符串,这会触发文本更改事件,该事件会执行else
第一个函数的块,当您聚焦或模糊时我不希望发生这种情况text 为空或搜索提示。
有没有办法当您聚焦或模糊输入字段时,由于聚焦或模糊事件,新文本为空或搜索提示,您可以阻止文本更改事件的发生?
谢谢。
function InitSearchBox(searchBoxID, searchButtonID, scope, searchLimit, treeviewID, searchErrorMessageBoxID, searchHint) {
// initializes the search button so it does the same thing as stop typing in the search box
$("#" + searchButtonID).click(function () {
var text = $("#" + searchBoxID).val();
alert(text);
if (text != "" && text != searchHint) {
RunSearch(text, scope, searchBoxID, searchLimit, treeviewID, searchErrorMessageBoxID);
} else {
$("#" + searchErrorMessageBoxID).html("");
RevealAllNodesOnTreeview(treeviewID);
}
});
// this part will make it so that after 1 second after the user stops typing in the search box, it will run the search request
var PDFSearchBoxTimeout = null;
$("#" + searchBoxID).bind("propertychange input", function () {
if (PDFSearchBoxTimeout !== null) {
clearTimeout(PDFSearchBoxTimeout);
}
PDFSearchBoxTimeout = setTimeout(function () {
$("#" + searchButtonID).click();
}, 1000);
});
}
function SetSearchBoxHint(searchHint, searchInputBoxID) {
// get the search box element
var my_element = $("#" + searchInputBoxID);
// this part occurs when you focus/blur on the search box, and will change its text and text colour depending on the scenario
$(my_element).focus(function () {
if ($(this).val() == searchHint) {
$(this).val("").css({ "color": "black" });
}
});
$(my_element).blur(function () {
if ($(this).val() == '') {
$(this).val(searchHint).css({ "color": "gray" });
}
});
// force the blur to occur on the search box
$(my_element).blur();
}
更新:
function SetSearchBoxHint(searchHint, searchInputBoxID) {
// get the search box element
var my_element = $("#" + searchInputBoxID);
// this part occurs when you focus/blur on the search box, and will change its text and text colour depending on the scenario
$(my_element).focus(function () {
if ($(this).val() == searchHint) {
$(my_element).prop('disabled', true);
$(this).val("").css({ "color": "black", "font-style": "normal" });
$(my_element).prop('disabled', false);
}
});
$(my_element).blur(function () {
if ($(this).val() == "") {
$(my_element).prop('disabled', true);
$(this).val(searchHint).css({ "color": "gray", "font-style": "italic" });
$(my_element).prop('disabled', false);
}
});
// force the blur to occur on the search box
$(my_element).blur();
}