我有一个文本框,正在使用实时搜索功能。测试HTML工作得很好,点击它就像它应该的那样添加到日志中。问题是,一旦我使用搜索框,新的“相同”代码被附加到#tag-result中,单击功能就停止工作了。我使用这段代码的相同变体来做其他事情,它一直工作得很好......除了这种情况。
例如,这段代码将“tag”关系替换为“user”,可以完美地工作。
// .blur was missed in original edit of this post...
$('#txttagSearch').blur(function() {
$(this).val('');
$('#tag-search-results').hide();
})
// remove above code and replace with the following
$(document).mouseup(function(e) {
$('#txtTagSearch').val('');
$('#tag-search-results').hide();
})
// now clicks register and search results hide when they should.
<div class="form-floating">
<input type="text" class="form-control" id="txtTagSearch" name="txtTagSearch" placeholder="Search to Add Tags" autocomplete="off">
<label for="txtTagSearch">Search to Add Tags</label>
</div>
<div class="tag-wrapper">
<div id="tag-search-results">
<ul id="tag-result">
<li class="tag-search-click 5">leather</li> /* this click works */
</ul>
</div>
<div class="row gx-2 mt-3">
<div class="col" id="tag-display">
<div class="tag" style="background-color: #000;">sunglasses</div>
</div>
</div>
</div>
$('#txtTagSearch').keyup(function() {
liveSearchTag($(this).val())
$('#tag-search-results').show();
})
$('#model-tag-results #tag-result').mousedown(function(e) {
e.preventDefault();
})
function liveSearchTag(keyword) {
$.ajax({
url: 'server/tag-search.php',
type: 'GET',
data: { keyword: keyword },
dataType: 'json',
success: function(result) {
const divTagResult = $('#tag-search-results #tag-result');
let html = '';
if (result !== null) {
if (result.length > 0) {
$.each(result, function(index, item) {
html += '<li class="tag-search-click ' + item['id'] + '">' + item['tag'] + '</li>' // these clicks do not detect?
})
} else {
let html = '<li style="padding: 10px;">NO RESULT FOUND</li>';
}
} else {
let html = '<li style="padding: 10px;">NO RESULT FOUND</li>';
}
divTagResult.html(html);
}
})
}
$('#tag-search-results').on('click','.tag-search-click',function() {
console.log('test');
var tagID = $(this).attr('class').replace('tag-search-click ','');
$.ajax({
url: 'server/tag-result.php',
type: 'GET',
data: { code: tagID },
dataType: 'json',
success: function(data) {
$('#tag-display').append('test');
$('#txtTagSearch').val('');
$('#txtTagSearch').blur();
$('#tag-search-results').hide();
}
})
})
已尝试使用多个单击方法...
$('#tag-search-results').on('click','.tag-search-click',function() {
$(document).on('click','.tag-search-click',function() {
$('.tag-search-click').(function() {
1条答案
按热度按时间gkn4icbw1#
我在原文章中错过了一些jQuery。点击时的.hide()并不是导致点击无法注册的原因。.blur()和.hide()是导致问题的原因。下面是我错过的错误代码,以及我现在用来隐藏和清除搜索结果的代码。现在一切都完美地工作。希望这能帮上忙。感谢VLAZ!!