Dies ist das JavaScript, das ich verwende. Es verwendet die Funktion „example_ajax_request“, die ich zu „functions.php“ in WordPress hinzugefügt habe. Sie können sie unterhalb des Skripts sehen.
Code: Select all
$('#select_show').change( function(){
var show = $("#select_show :selected").text();
$.ajax({
type: "post",
url: ajaxurl,
data: {
'action':'example_ajax_request',
'show' : show
},
dataType:"json",
success:function(data) {
console.log("what is happening al of a sudden");
console.log(data);
$('#province').children('option').remove();
for(var i = 0; i < data.length; i++)
{
if( data[i] == 'Choose a province')
var value = "all";
else
var value = data[i];
$('#province').append( $("").text(data[i]) );
}
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
Code: Select all
function example_ajax_request() {
$arrayList = array();
$arrayList[] = "Choose a province";
$selected = $_POST['show'];
if( $selected == "Choose a show" )
$selected = "";
$arg = array(
'posts_per_page' => -1,
'post_type' => 'address',
'meta_key' => 'show',
'meta_value' => $selected,
);
$the_query = new WP_Query( $arg );
if( $the_query->have_posts() )
{
while ( $the_query->have_posts() )
{
$the_query->the_post();
$arrayList = get_option_list('province', $arrayList);
}
}
print json_encode($arrayList);
wp_reset_query();
die();
}
add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
Code: Select all
Choose a show
Mobile version