如何使用ajax搜索输入字段中的精确短语或单词?
问题描述:
我一直在following this,它几乎完美的工作,但它不仅找到与我们在输入字段中编写的相同单词的标题帖子,而且它无论如何查找所有帖子。如何使用ajax搜索输入字段中的精确短语或单词?
HTML
<input name="usp-title" id="usp-title" type="text" value="" data-required="true" required="required" maxlength="99" placeholder="Type here..." class="usp-input usp-input-title form-control">
AJAX
add_action('wp_footer', 'ajax_fetch');
function ajax_fetch() {
?>
<script type="text/javascript">
function fetch(){
console.log(jQuery('#usp-title').val());
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', exactwords: jQuery('#usp-title').val() },
success: function(data) {
var text1;
var text1B;
var text2;
var text2B;
jQuery("#datafetch").html(data).promise().done(function(){
text1 = jQuery("#datafetch").find("h2").find("a").html();
text1B = text1.toLowerCase();
text2 = jQuery('#usp-title').val();
text2B = text2.toLowerCase();
console.log(text1B);
console.log(text2B);
if (text1B != text2B) {
jQuery("#componi").removeAttr("disabled", "disabled").show();
jQuery("#fatto").hide();
//jQuery('#datafetch').empty();
} else if (text1B == text2B) {
jQuery("#componi").attr("disabled", "disabled").hide();
}
});
}
});
}
</script>
PHP
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query(array('posts_per_page' => 1, 's' => esc_attr($_POST['usp-title']), 'post_type' => 'post'));
if($the_query->have_posts()) : ?>
<div class="jumbotron">
<h4>Abbiamo trovato già delle lezioni con lo stesso titolo,<br>usa una di questa o scrivi un titolo diverso.</h4>
</div>
<ul class="list-unstyled margin-top-80">
<?php while($the_query->have_posts()): $the_query->the_post(); ?>
<li>
<?php $time = usp_get_meta(false, 'usp-custom-10'); ?>
<?php $space = usp_get_meta(false, 'usp-custom-3'); ?>
<?php $spaceB = usp_get_meta(false, 'usp-custom-7'); ?>
<h2><a target="_blank" href="<?php echo esc_url(post_permalink()); ?>"><?php the_title();?></a></h2>
<p><strong>Spazio:</strong> <?php echo $space;?> <?php echo $spaceB; ?></p>
<p><strong>Tempo:</strong> <?php echo $time; ?></p>
<?php the_excerpt(); ?>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif;
die();
}
如何限制的结果,只有一个与输入字段相同的标题按文本?
答
这是更多的黑客,但我找不到任何其他方式,尝试exact= "true"
和sentence="true"
作为查询中的参数,但仍然,我有所有的结果不仅是完全匹配的。因此,因为我是有这个结果作为list
,我做的DOM
检查和消除任何不完全匹配.val()
function fetch(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', exactwords: jQuery('#usp-title').val() },
success: function(data) {
var text2;
var text2B;
text2 = jQuery('#usp-title').val();
text2B = text2.toLowerCase();
jQuery("#datafetch").html(data).promise().done(function(){
jQuery("#datafetch ul li h2 a").each(function() {
var $this = jQuery(this);
if ($this.text().toLowerCase() !== text2B) {
$this.parent().parent().remove();
jQuery(".jumbotron").remove();
jQuery("#componi").removeAttr("disabled", "disabled").show();
} else if ($this.text().toLowerCase() == text2B) {
jQuery("#componi").attr("disabled", "disabled").hide();
}
});
});
}
});
}