向自定义重力场添加自动完成功能不起作用

时间:2016-01-25 作者:Mahmood Rehman

我正在尝试将自动完成添加到重力表单上的自定义字段中。但它不起作用。数据返回,但未显示在字段上。我是这样做的:

(function( jQuery ) {
    jQuery(function() {
        var url = ajaxurl + "?action=my_search";
        jQuery( "#input_22_2" ).autocomplete({
            source: url,
            minLength: 2
        }); 
    });

})( jQuery );
PHP代码:

function my_search() {
   global $wpdb;
        $term = strtolower( $_GET[\'term\'] );



         $query = $wpdb->get_results( "SELECT * FROM wp_vhh4zdnv0r_disease WHERE scifi_name like \'%".$_GET[\'term\']."%\' OR hospital_code like \'%".$_GET[\'term\']."%\' OR layman_language like \'%".$_GET[\'term\']."%\'");
    if ( $query )
    {
        foreach ( $query as  $valuek) {

           if(strpos($valuek->scifi_name,$_GET[\'term\']) !== false){
            $row[] = $valuek->scifi_name;
           }


        }
    }

        $response = json_encode( $row );
        echo $response;
        exit();

}

    add_action( \'wp_ajax_my_search\', \'my_search\' );

1 个回复
最合适的回答,由SO网友:Mahmood Rehman 整理而成

我已经用下面的代码解决了我的问题。所以可能对某人有帮助。我尝试了自动完成的引用样式

<script>

    var locations = \'<?php echo $json;?>\';
var locations_array = JSON.parse(locations);
 locations_array.sort();


    function monkeyPatchAutocomplete2() {
            jQuery("#result").remove();
          // Don\'t really need to save the old fn, 
          // but I could chain if I wanted to
          var oldFn = jQuery.ui.autocomplete.prototype._renderItem;

          jQuery.ui.autocomplete.prototype._renderItem = function( ul, item) {
              var re = new RegExp( "\\\\b" + this.term, "i") ;
              var t = item.label.replace(re,"<span class=\'highlighted\'>" + this.term + "</span>");
              return jQuery( "<li></li>" )
                  .data( "item.autocomplete", item )
                  .append( "<a>" + t + "</a>" )
                  .appendTo( ul );

          };
      }


      jQuery(document).ready(function() {

        jQuery(\'.medication input\').live(\'keyup\', function (event) {

    if(this.value.length < 2){
        jQuery("#result").remove();
    } 

          monkeyPatchAutocomplete2();
          //jQuery("#result").remove();
           jQuery( ".medication input" ).autocomplete({

            matchContains: true,
            multiselect: true,            
             minLength: 2,
              source: function(req, responseFn) {

                  //addMessage("search on: \'" + req.term + "\'<br/>");
                  var re = jQuery.ui.autocomplete.escapeRegex(req.term);
                  var matcher = new RegExp(  "\\\\b" + re, "i" );
                  var a = jQuery.grep( locations_array, function(item,index){
                      //addMessage("&nbsp;&nbsp;sniffing: \'" + item + "\'<br/>");
                      return matcher.test(item);
                  }); 


                  responseFn( a );
              },
              focus: function (event, ui) {

                jQuery("#result").remove();
                return false;
            },
          });


});


 </script>

相关推荐

JQuery php请求返回一个奇怪的结果

我有一个奇怪的小故障发生在我身上,我不知道我是如何产生它的,或者它是否是正常的。我正在开发自己的插件,当一个足球队/足球队被输入到一个框中时,它会检查它是否已经在数据库中。以下是我的代码行add_action( \'admin_footer\', \'fws_teamcheck_javascript\' ); function fws_teamcheck_javascript() { ?> <script type="text/javascript">