Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ajax搜索输入字段中的确切短语或单词?_Ajax_Wordpress - Fatal编程技术网

如何使用ajax搜索输入字段中的确切短语或单词?

如何使用ajax搜索输入字段中的确切短语或单词?,ajax,wordpress,Ajax,Wordpress,我一直是,它几乎完美地工作,但它不仅是找到标题文章与相同的话,根据我们写在输入字段,但它找到所有的职位,不管 HTML 阿贾克斯 add_操作('wp_footer','ajax_fetch'); 函数ajax_fetch(){ ?> 函数fetch(){ log(jQuery('#usp title').val()); jQuery.ajax({ url:“”, 键入:“post”, 数据:{action:'data_fetch',exactwords:jQuery('#usp title

我一直是,它几乎完美地工作,但它不仅是找到标题文章与相同的话,根据我们写在输入字段,但它找到所有的职位,不管

HTML


阿贾克斯

add_操作('wp_footer','ajax_fetch');
函数ajax_fetch(){
?>
函数fetch(){
log(jQuery('#usp title').val());
jQuery.ajax({
url:“”,
键入:“post”,
数据:{action:'data_fetch',exactwords:jQuery('#usp title').val()},
成功:功能(数据){
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);
如果(text1B!=text2B){
jQuery(“#componi”).removeAttr(“disabled”、“disabled”).show();
jQuery(“#fatto”).hide();
//jQuery('#datafetch').empty();
}else if(text1B==text2B){
jQuery(“#componi”).attr(“disabled”、“disabled”).hide();
}
});
}
});
}
PHP

add_action('wp_ajax_data_fetch','data_fetch');
添加操作('wp_ajax_nopriv_data_fetch','data_fetch');
函数数据_fetch(){
$the_query=new WP_query(数组('posts_per_page'=>1,'s'=>esc_attr($_POST['usp-title']),'POST_type'=>'POST');
如果($the_query->have_posts()):?>
阿比亚莫·特罗瓦托(Abbiamo trovato giádele lezioni con o stesso titolo)是美国的一名律师。
  • Spazio:

    节奏:


这更像是一种黑客行为,但我找不到任何其他方法,尝试了
exact=“true”
句子=“true”
作为查询中的参数,但我仍然拥有所有结果,而不仅仅是完全匹配的结果。因此,由于我将结果作为
列表
,因此我正在
DOM
中进行检查,并删除与
.val()不完全匹配的任何内容。

函数fetch(){
jQuery.ajax({
url:“”,
键入:“post”,
数据:{action:'data_fetch',exactwords:jQuery('#usp title').val()},
成功:功能(数据){
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”)。每个(函数(){
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();
}
});
});
}
});
}
<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">
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>
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();
}
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();
          }
        });
      });
    }
  });
}