在Wordpress AJAX中应用Chosen Select Plugin

时间:2014-02-09 11:05:56

标签: javascript php ajax wordpress jquery-chosen

P.S。这个问题很长,因为有很多代码。感谢您的耐心和时间。我很感激。
我在wordpress网站上使用ultimate wp query search filter插件。我想使用选择的插件而不是默认的选择框。页面上已有的选择框与此代码完美配合。

(function($){
$(".first-ddwn, .second-ddwn, .third-ddwn, .main-ddwn, .posts_ajax select").chosen({
no_results_text: "Oops, nothing found!",
width: "50%"
});
})(jQuery); 

我已经更改了插件作者提到的AJAX搜索结果html,以便我得到结果帖的下拉列表。

add_filter('uwpqsf_result_tempt', 'customize_output', '', 4);
function customize_output($results , $arg, $id, $getdata ){
 // The Query
        global $post;
        $apiclass = new uwpqsfprocess();
         $query = new WP_Query( $arg );
    ob_start();    $result = '';?>
    <form action="<? bloginfo('url'); ?>" method="get"><?php
    echo '<select name="page_id" id="page_id">';
    echo '<option value="">Select</option>';
        // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post(); ?>
                            <option value="<? echo $post->ID; ?>"><?php echo the_title(); ?></option>
     <?php   }
   echo '</select>';  
   echo '<input type="submit" name="submit" value="view" />';
   echo '</form>';
     } else {
                 echo  'no post found';
            }
            /* Restore original Post Data */
            wp_reset_postdata();

    $results = ob_get_clean();        
        return $results;
}

但是,选择的结果不适用于此结果下拉列表。我已经尝试了在提到http://davidwalsh.name/jquery-chosen#comment-29299的AJAX调用之后使用settimeout的选项,但它没有用。也许我做错了。此外,当我在下面的ajax调用之后运行它时,选择确实应用于选择,但都被打破了。

jQuery.ajax({
             type: 'POST',   
             url: ajax.url,
             timeout:3000,
             data: ({action : 'uwpqsf_ajax',getdata:getdata, pagenum:pagenum }),
             beforeSend:function() {$(''+ajxdiv+'').empty();res.container.append(res.loader);},
             success: function(html) {
                res.container.find(res.loader).remove();
              $(''+ajxdiv+'').html(html);
              $(".content select").chosen();
             }
             });

这就是我得到的 enter image description here enter image description here enter image description here

这是插件的uwpqsfscript.js文件

jQuery(document).ready(function($) {

    $('body').on('click','.usearchbtn', function(e) {
        process_data($(this));
        return false;
    });

    $('body').on('click','.upagievent', function(e) {
        var pagenumber =  $(this).attr('id');
        var formid = $('#curuform').val();
        upagi_ajax(pagenumber, formid);
        return false;
    });

    $('body').on('keypress','.uwpqsftext',function(e) {
      if(e.keyCode == 13){
        e.preventDefault();
        process_data($(this));
      }
    });

    window.process_data = function ($obj) {

        var ajxdiv = $obj.closest("form").find("#uajaxdiv").val();  
        var res = {loader:$('<div />',{'class':'umloading'}),container : $(''+ajxdiv+'')};

        var getdata = $obj.closest("form").serialize();
        var pagenum = '1';

        jQuery.ajax({
             type: 'POST',   
             url: ajax.url,
             timeout:3000,
             data: ({action : 'uwpqsf_ajax',getdata:getdata, pagenum:pagenum }),
             beforeSend:function() {$(''+ajxdiv+'').empty();res.container.append(res.loader);},
             success: function(html) {
                res.container.find(res.loader).remove();
              $(''+ajxdiv+'').html(html);

             }
             });

    }   

    window.upagi_ajax = function (pagenum, formid) {
        var ajxdiv = $(''+formid+'').find("#uajaxdiv").val();   
        var res = {loader:$('<div />',{'class':'umloading'}),container : $(''+ajxdiv+'')};
        var getdata = $(''+formid+'').serialize();

        jQuery.ajax({
             type: 'POST',   
             url: ajax.url,
             data: ({action : 'uwpqsf_ajax',getdata:getdata, pagenum:pagenum }),
             beforeSend:function() {$(''+ajxdiv+'').empty(); res.container.append(res.loader);},
             success: function(html) {
              res.container.find(res.loader).remove();
              $(''+ajxdiv+'').html(html);

            //res.container.find(res.loader).remove();
             }
             });
    }

 $('body').on('click', '.chktaxoall,.chkcmfall',function () {

    $(this).closest('.togglecheck').find('input:checkbox').prop('checked', this.checked);

     });

});//end of script

来自uwpqsf-process-class.php的一些相关代码(可能是?)

//front ajax
     add_action( 'wp_ajax_nopriv_uwpqsf_ajax', array($this, 'uwpqsf_ajax') );
     add_action( 'wp_ajax_uwpqsf_ajax', array($this, 'uwpqsf_ajax') );
     add_action( 'pre_get_posts', array($this ,'uwpqsf_search_query'),1000);
    }
function uwpqsf_ajax(){
$postdata =parse_str($_POST['getdata'], $getdata);
$taxo = (isset($getdata['taxo']) && !empty($getdata['taxo'])) ? $getdata['taxo'] : null;
$cmf = (isset($getdata['cmf']) && !empty($getdata['cmf'])) ? $getdata['cmf'] : null;
$formid = $getdata['uformid'];
$nonce =  $getdata['unonce'];
$pagenumber = isset($_POST['pagenum']) ? $_POST['pagenum'] : null;

if(isset($formid) && wp_verify_nonce($nonce, 'uwpsfsearch')){
    $id = absint($formid);
    $options = get_post_meta($id, 'uwpqsf-option', true);
    $cpts = get_post_meta($id, 'uwpqsf-cpt', true);
    $pagenumber = isset($_POST['pagenum']) ? $_POST['pagenum'] : null;
    $default_number = get_option('posts_per_page');

    $cpt        = !empty($cpts) ? $cpts : 'any';
    $ordermeta  = !empty($options[0]['smetekey']) ? $options[0]['smetekey'] : null;
    $ordertype = (!empty($options[0]['otype']) ) ? $options[0]['otype'] : null;
    $order      = !empty($options[0]['sorder']) ? $options[0]['sorder'] : null;
    $number      = !empty($options[0]['resultc']) ? $options[0]['resultc'] : $default_number;

    $keyword = !empty($getdata['skeyword']) ? sanitize_text_field($getdata['skeyword']) : null;
    $get_tax = $this->get_uwqsf_taxo($id, $taxo);
    $get_meta = $this->get_uwqsf_cmf($id, $cmf);

    if($options[0]['snf'] != '1' && !empty($keyword)){
     $get_tax = $get_meta = null;
    }

    $ordermeta  = apply_filters('uwpqsf_ometa_query',$ordermeta,$getdata,$id);
    $ordervalue = apply_filters('uwpqsf_ometa_type',$ordertype,$getdata,$id);
    $order      = apply_filters('uwpqsf_order_query',$order,$getdata,$id);  
    $number     = apply_filters('uwpqsf_pnum_query',$number,$getdata,$id);  



    $args = array(
        'post_type' => $cpt,
        'post_status' => 'publish',
        'meta_key'=> $ordermeta,
        'orderby' => $ordertype,
        'order' => $order, 
        'paged'=> $pagenumber,
        'posts_per_page' => $number,
        'meta_query' => $get_meta,                      
        'tax_query' => $get_tax,
        's' => esc_html($keyword),
        );

    $arg = apply_filters( 'uwpqsf_query_args', $args, $id,$getdata);        



    $results =  $this->uajax_result($arg, $id,$pagenumber,$getdata);
    $result = apply_filters( 'uwpqsf_result_tempt',$results , $arg, $id, $getdata );    

    echo $result;
    }else{ echo 'There is error here';}
        die;    



 }//end ajax    

  function uajax_result($arg, $id,$pagenumber,$getdata){
        $query = new WP_Query( $arg );
    $html = '';
        //print_r($query);  // The Loop
    if ( $query->have_posts() ) {
      $html .= '<h1>'.__('Search Results :', 'UWPQSF' ).'</h1>';
       while ( $query->have_posts() ) {
                $query->the_post();global $post;
            $html .= '<article><header class="entry-header">'.get_the_post_thumbnail().'';
            $html .= '<h1 class="entry-title"><a href="'.get_permalink().'" rel="bookmark">'.get_the_title().'</a></h1>';
            $html .= '</header>';
            $html .= '<div class="entry-summary">'.get_the_excerpt().'</div></article>';

          }
            $html .= $this->ajax_pagination($pagenumber,$query->max_num_pages, 4, $id,$getdata);
         } else {
                    $html .= __( 'Nothing Found', 'UWPQSF' );
                }
                /* Restore original Post Data */
                wp_reset_postdata();

        return $html;


  }//end result  

我一直在寻找好几天,并尝试过许多方法来完成这项工作。我不知道jquery,所以也许有人会帮助我。 非常感谢

0 个答案:

没有答案