Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery(document).ready(function($){
- if (typeof acf == 'undefined') { return; }
- var acf_field = acf.getField('field_579376f522130');
- acf_field.on("change ready", function(e){
- // bail early if no ajax
- if( !acf.get('ajax') ) return;
- // abort XHR if it's already running
- if( this.request ) {
- this.request.abort();
- }
- // set the ajax action that's set up in php
- var data = {
- action: 'load_city_field_choices',
- key_name: acf_field.val(); //The key_name needs to be the name of the parameter in your action
- }
- this.request = $.ajax({
- url: acf.get('ajaxurl'), // ajax url, acf already sets this
- data: acf.prepareForAjax(data), // another included acf function
- type: 'post', // posted values, look at $_POST in your php ajax action function
- dataType: 'json', // how we want the date returned
- success: function( json ){
- // get the field we want to populate
- var $select = $('.acf-field-5793770922131 select');
- // this stores the currenlty selected value in that field
- // so that we can mark it as selected, this is important when loading
- // and exsiting post
- var $selected = $select.val();
- // clear all of the exsiting options from the field
- $select.empty();
- // rebuild the child select field
- $select.append('<option data-i="" selected="selected" value="">- Select -</option>');
- var count = json.length;
- for (i=0; i<count; i++) {
- var $item = '<option value="'+json[i]['value']+'"'
- if ($selected == json[i]['value']) {
- $item += ' selected="selected"';
- }
- $item += '>'+json[i]['label']+'</option>';
- $select.append($item);
- }
- }
- }); // end ajax
- });
- // trigger the ready action to load the field
- // this is important for existing posts
- $('#acf-field_579376f522130').trigger('ready');
- });
Advertisement
Add Comment
Please, Sign In to add comment