问题
On WordPress is use Ninja Forms. i have a page with multiple Date Fields in the same Form ( date of arrival and date of departure). I need to change the Value of both Date fields.
the following example script works (i can add jQuery to the page by a WP plugin), but is changing the value of both fields:
<script>
$( document ).ready( function() {
new(Marionette.Object.extend( {
initialize: function() {
this.listenTo( Backbone.Radio.channel( 'pikaday' ), 'init', this.modifyDatepicker );
},
modifyDatepicker: function( dateObject, fieldModel ) {
dateObject.pikaday.setDate( '04/11/2016' );
}
}));
});
</script>
i don't know how to address the 2 fields separately. The 2 fields created by Ninja Forms look like this (they differ only by the ID):
<div class="nf-field-element">
<div class="pikaday__container"><input id="nf-field-66" name="nf-field-66" class="ninja-forms-field nf-element datepicker" value="11/04/2016" type="hidden">
<input class="pikaday__display pikaday__display--pikaday ninja-forms-field nf-element datepicker" placeholder="" type="text">
</div></div>
Any idea how to adjust the jQuery?
回答1:
I just had to fix the exact same problem for a client. Although I did not fix it via jQuery my solution might help you - or anyone else with a similar problem:
I had to pre-populate my datepickers with values defined in query-strings and did it the following way:
- Add a filter that hooks into 'ninja_forms_render_default_value'
- Filter for 'date' field type
- Check for a date field with a given key
- Check if query-string is set
To achieve the same you can just copy this into your functions.php:
add_filter( 'ninja_forms_render_default_value', 'my_change_nf_default_value', 10, 3 );
function my_change_nf_default_value( $default_value, $field_type, $field_settings ) {
if ('date' == $field_type ) {
if ('date_from_query_string' == $field_settings['key']) {
if (!empty( get_query_var('date_from') )) {
return get_query_var('date_from');
}
}
if ('date_from_query_string' == $field_settings['key']) {
if (!empty( get_query_var('date_to') )) {
return get_query_var('date_to');
}
}
}
}
来源:https://stackoverflow.com/questions/43389064/addressing-and-setting-2-datepicker-fields-on-wordpress-with-ninja-forms