Php 动态选择下拉列表Wordpress By advanced custom field
这是我的多重下拉代码 我有两个下拉列表,一个用于城市,另一个用于区域 所以当有人选择一个城市时,区域下拉列表应该会自动填充 这是urlPhp 动态选择下拉列表Wordpress By advanced custom field,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,这是我的多重下拉代码 我有两个下拉列表,一个用于城市,另一个用于区域 所以当有人选择一个城市时,区域下拉列表应该会自动填充 这是url 选择区域 在这种情况下,您可能需要在代码中使用一些ajax,以便在更改第一个下拉菜单的值时,它会触发ajax调用。要实现这一点,您必须包含一些类似这样的javascript $('#city_dropdown').change(function() { jQuery.ajax({ url: "/wp-admin/admin-ajax.
选择区域
在这种情况下,您可能需要在代码中使用一些ajax,以便在更改第一个下拉菜单的值时,它会触发ajax调用。要实现这一点,您必须包含一些类似这样的javascript
$('#city_dropdown').change(function() {
jQuery.ajax({
url: "/wp-admin/admin-ajax.php",
type: 'POST',
data: {
action: 'get_city_areas',
city_id: $(this).val(),
},
dataType: 'html',
success: function (result) {
$('#area-dropdown').html(result)
},
error: function (errorThrown) {
console.log(errorThrown);
}
})
});
这里发生的事情是,当您的城市下拉列表被更改时,它会触发一个Ajax请求来获取所有相关区域。Wordpress中的Ajax是通过管理Ajax系统完成的。您可以在此处阅读更多有关内容:
在functions.php文件中,您可以添加以下内容来注册Ajax调用以检索城市区域
add_action('wp_ajax_get_city_areas', 'handle_get_city_areas');
add_action('wp_ajax_nopriv_get_city_areas', 'handle_get_city_areas');
/**
* Handle the list of areas
*/
function handle_get_city_areas(){
$city_id = $_POST['city_id'];
//Using the city ID, retrieve the related areas
//and then echo the new select menu
}
这将有助于您获得所需信息。您的任何问题都是…?请尊重这里试图帮助您的用户,并在重新格式化代码方面付出一些努力。我已重新格式化了它。。。请现在检查
add_action('wp_ajax_get_city_areas', 'handle_get_city_areas');
add_action('wp_ajax_nopriv_get_city_areas', 'handle_get_city_areas');
/**
* Handle the list of areas
*/
function handle_get_city_areas(){
$city_id = $_POST['city_id'];
//Using the city ID, retrieve the related areas
//and then echo the new select menu
}