Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 动态选择下拉列表Wordpress By advanced custom field_Php_Wordpress_Advanced Custom Fields - Fatal编程技术网

Php 动态选择下拉列表Wordpress By advanced custom field

Php 动态选择下拉列表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.

这是我的多重下拉代码

我有两个下拉列表,一个用于城市,另一个用于区域

所以当有人选择一个城市时,区域下拉列表应该会自动填充

这是url


选择区域

在这种情况下,您可能需要在代码中使用一些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
}