2017-08-23 7 views
0

ボタン値を取得することで問題が発生しています。 phpのデータをソートするために、クリック時にButton値を取得する必要があります。クリック時のボタン値を取得する方法ajax

もし私がいくつか試してみるとexamplesすべて正常に動作します。

クリックしたボタンから値を取得する方法がわかりません。 配列をソートするには、これが必要です。誰かがajaxによってクリックされたボタンの価値を得る方法を教えてもらえますか? PHPで

jQuery(document).ready(function($) { 
    $("#button1").click(function() { 
       var data = { 
        action: 'table_contest', 
        security : MyAjax.security, 
        sort: 1 
       }; 

       // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php 
       $.post(MyAjax.ajaxurl, data, function(response) { 
        //test 
        alert('Got this from the server: ' + response); 
       }); 
      }); 
}); 

::これは私のjQueryのです

アイデアを おかげ..

// Add the JS 
function theme_name_scripts() 
{ 
    wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom.js', array('jquery'), '1.0.0', true); 
    wp_localize_script('custom-script', 'MyAjax', array(
     // URL to wp-admin/admin-ajax.php to process the request 
     'ajaxurl' => admin_url('admin-ajax.php'), 
     // generate a nonce with a unique ID "myajax-post-comment-nonce" 
     // so that you can check it later when an AJAX request is sent 
     'security' => wp_create_nonce('test') 
    )); 
} 

add_action('wp_enqueue_scripts', 'theme_name_scripts'); 


function table_contest_function($atts){ 

    check_ajax_referer('test', 'security'); 

    //get how to sort the tabledata 
    $sort = intval($_POST['sort']); 


    $atts = shortcode_atts(
     array(
      'table-id' => '' 
     ), $atts); 

    // get table data from Plugin TablePress 
    $table = TablePress::$model_table->load($atts['table-id'], true, true); 

    //only get the important data 
    $data = $table['data']; 

//if button was clicked sort asc 
    if ($sort == '1'){ 
     //sort 
asort($data); 
    } 

    $output = ......; 


    echo $output; 
} 

add_action('wp_ajax_table_contest', 'table_contest_function'); 
+0

クリック機能... $(this).val()OR $(this).html – Roy

答えて

2

はあなただけ取得します。このキーワードを使用してクリックした要素を、取得する必要があります現在の範囲。

以下のコードを確認してください。

jQuery(document).ready(function($) { 
    $("#button1").click(function() { 
       var btn_val = $(this).val(); 
       var data = { 
        action: 'table_contest', 
        security : MyAjax.security, 
        sort: 1, 
        btn : btn_val 

       }; 

       // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php 
       $.post(MyAjax.ajaxurl, data, function(response) { 
        //test 
        alert('Got this from the server: ' + response); 
       }); 
      }); 
}); 
+0

ありがとう! :) – azrm

関連する問題