我正在使用下面的脚本来搜索我的 HTML 网站。
我遇到的问题是,当我希望它在整个 XML 中独立搜索单词时,它会将单词作为一个短语一起搜索。即,如果有人输入“数字制作人”,它会搜索“数字制作人”,而不是单独搜索“数字”和“制作人”这两个词。
任何有关使其工作的帮助将不胜感激!
$(document).ready(function(){
//GLOBAL VAR
var XMLSource = $('#data').attr('xmlData');
var keyword = '';
var catType = '';
var pub = '';
var i = 0;
$("#searchButton").click(function() {
keyword = $("input#term").val();
catType = $("#category option:selected").val();
//Reset any message
var errMsg = '';
pub = '';
if ( keyword == '' ) { errMsg += 'Please Enter Search Term' + '\n'; }
else if ( catType == 'none' ) { errMsg += 'Please Select Category' + '\n'; }
else { searchThis(); }
if ( errMsg != '' ) {
pub += '<div class="error">' + '\n';
pub += errMsg;
pub += '</div>' + '\n';
}
//Show error
$('#result').html( pub );
});
function searchThis() {
$.ajax({
type: "GET",
url: XMLSource,
dataType: "xml",
success: function(xml) { loadPublication (xml) }
});
}
function loadPublication (xmlData) {
i = 0;
var row;
var searchExp = "";
$(xmlData).find('Job').each(function(){
var SearchTitle = $(this).find('SearchTitle').text();
var Description = $(this).find('Description').text();
var EmailTo = $(this).find('EmailTo').text();
//Format the keyword expression
var exp = new RegExp(keyword,"gi");
//Check if there is a category selected;
//if not, use EmailTo column as a default search
if ( catType == 'SearchTitle' ) { searchExp = SearchTitle.match(exp); }
else if ( catType == 'Description' ) { searchExp = Description.match(exp); }
else if ( catType == 'EmailTo' ) { searchExp = EmailTo.match(exp); }
if ( searchExp != null ) {
//Start building the result
if ((i % 2) == 0) { row = 'even'; }
else { row = 'odd'; }
i++;
pub += '<tr class="row ' + row + '">' + '\n';
pub += '<td valign="top" class="col1">' + SearchTitle + '</td>' + '\n';
pub += '<td valign="top" class="col2">' + Description + '</td>' + '\n';
pub += '<td valign="top" class="col3">' + EmailTo + '</td>' + '\n';
pub += '</tr>' + '\n';
}
});
if ( i == 0 ) {
pub += '<div class="error">' + '\n';
pub += 'No Result Found' + '\n';
pub += '</div>' + '\n';
//Populate the result
$('#result').html( pub );
}
else {
//Pass the result set
showResult ( pub );
}
}
function showResult (resultSet) {
//Show the result
pub = '<table id="grid" border="0">' + '\n';
pub += '<thead><tr>' + '\n';
pub += '<th class="col1">Job Title</th>' + '\n';
pub += '<th class="col2">Job Description</th>' + '\n';
pub += '<th class="col3">Apply To</th>' + '\n';
pub += '</tr></thead>' + '\n';
pub += '<tbody>' + '\n';
pub += resultSet;
pub += '</tbody>' + '\n';
pub += '</table>' + '\n';
//Populate
$('#result').html(pub)
$('#grid').tablesorter();
}
});