Passer la navigation

[Résolu] Set a field in a form to a static value based on a dropdown in the same form

This support ticket is created Il y a 4 years, 6 months. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Fuseau horaire du supporter : Asia/Hong_Kong (GMT+08:00)

Ce sujet contient 5 réponses, a 3 voix.

Dernière mise à jour par trulsD Il y a 4 years, 6 months.

Assisté par: Luo Yang.

Auteur
Publications
#2223963

I am trying to set the value of a hidden field in a form to a static value based on a selection in a relationship dropdown in the same form. What is the best approach to achieve this?

Truls

#2224081

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+01:00)

You'll need some custom JS (or jQuery, which will be available on the page) to achieve this.

You'll want to add an event listener to the relationship dropdown to listen for when its value changes, and then update your hidden field as required when it does.

With Forms you should wait for the window.load event before running your code, so the bare bones of how you would add your code (which you can do in the Custom JS section of your form) would be something like:

window.onload = function ($) {
    
    // your code here
    
    $('selector for relationship dropdown').change( function(){

        // value of dropdown available with $(this).val() if needed
        // set value of hidden field with $('selector for hidden field').val( someValue )
    });

}(jQuery);

See
lien caché
lien caché

#2229941

Hi
I get the code to work for input and category select fields. The code does not work when I try it on the relationship dropdown.
When I look at the HTML code, the relationship dropdown does not look like a regular dropdown select.
What do I use as a selector in the JQuery code for the relationship dropdown?
Truls

#2230205

Hi
Here is the HTML code for the combobox:

<div class="js-wpt-field-items js-wpt-repetitive wpt-repetitive" data-initial-conditional="" data-item_name="select-@avtalestatus.parent">
<select id="cred_form_97_1_1_avtalestatusparent" preset_value="" urlparam="" make_readonly="" max_width="" max_height="" class="form-control wpt-form-select form-select select toolset_select2_prefix_40197 toolset_select2_converted toolset_select2-hidden-accessible" output="bootstrap" select_text="Ikke
                angitt" data-orderby="title" data-order="ASC" data-author="" data-wpt-type="select" name="@avtalestatus.parent" tabindex="-1" aria-hidden="true">
<option value="" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_97_1_1_cred_form_97_1_1_avtalestatusparent" data-wpt-name="@avtalestatus.parent">Ikke
                angitt</option>
<option value="1069">Under registrering</option></select><span class="toolset_select2 toolset_select2-container toolset_select2-container--default toolset_select2-container--focus" dir="ltr" style="width: 275.047px;"><span class="selection"><span class="toolset_select2-selection toolset_select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="toolset_select2-cred_form_97_1_1_avtalestatusparent-container"><span class="toolset_select2-selection__rendered" id="toolset_select2-cred_form_97_1_1_avtalestatusparent-container" title="Under registrering"><span class="toolset_select2-selection__clear">×</span>Under registrering</span><span class="toolset_select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
</div>
#2230501

Hello,

Since it is a custom JS codes problem, if you need more assistance for it, please provide a test site with the same problem, also point out the problem page URL and form URL, I need to test and debug it in a live website, thanks

#2235353

My issue is resolved now. Thank you!