用重力形式的另一个字段值填充一个字段

Populate a field with another field value in gravity forms

我目前在我的 Wordpress 网站上有一个简单的重力表单,其中包含一个带有 3 个选项的单选按钮字段:(单身、已婚、豁免)。

如果用户在单选按钮字段中选择免除选项,我想在同一表单上动态填充值为 "Exempt" 的单独隐藏文本字段。如果用户选择其他选项之一(单身或已婚),我希望文本字段中的值为 NULL。

有没有人以前做过类似的事情,如果有,你能帮我完成这个步骤吗?

您需要将代码添加到自定义 JavaScript 文件中。您可以阅读 enqueuing JavaScript files here。在您的评论中,您只为 radio 输入提供了一个 ID,但每个输入都应该有自己的唯一 ID。因此,假设您在此表单上唯一的无线电输入是这 3 个输入,您也可以按 type 定位(正如我在下面所做的那样)。如果您想分享您的表单 HTML,我可以根据您的具体情况定制答案。

// Document ready function for Wordpress
jQuery(function($) {
  $('form#25 input[type="radio"]').change(function(){
    if (this.value === 'exempt') {
      $('input#26').val(this.value);
    } else {
      $('input#26').val(null);
    }
  })
});
label {
  display:block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="25">
  <label>Single<input type="radio" name="marriage-status" value="single" /></label>
  <label>Married<input type="radio" name="marriage-status" value="married" /></label>
  <label>Exempt<input type="radio" name="marriage-status" value="exempt" /></label>
  <!-- HIDDEN INPUT -->
  <input id="26" type="text" />
</form>

看起来您的表单只有 radio 个 "Filing Status," 输入,因此您可以这样做:

// Document ready function for Wordpress
jQuery(function($) {
  $('form#gform_25 input[type="radio"]').change(function(){
    if (this.value === 'Exempt') {
      $('input#input_25_24_valid').val(this.value);
    } else {
      $('input#input_25_24_valid').val(null);
    }
  })
});
ul {
  padding-left:0;
}
li {
  list-style-type:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="gform_25">
  <li id="field_25_9" class="gfield gfield_contains_required field_sublabel_below field_description_below gfield_visibility_visible">
     <label class="gfield_label">Filing Status<span class="gfield_required">*</span></label>
     <div class="ginput_container ginput_container_radio">
        <ul class="gfield_radio" id="input_25_9">
           <li class="gchoice_25_9_0"><input name="input_9" type="radio" value="Single or Married filing sperately" id="choice_25_9_0"><label for="choice_25_9_0" id="label_25_9_0">Single or Married filing sperately</label></li>
           <li class="gchoice_25_9_1"><input name="input_9" type="radio" value="Married filing jointly" id="choice_25_9_1"><label for="choice_25_9_1" id="label_25_9_1">Married filing jointly (or Qualifying widow(er))</label></li>
           <li class="gchoice_25_9_2"><input name="input_9" type="radio" value="Head of household" id="choice_25_9_2"><label for="choice_25_9_2" id="label_25_9_2">Head of household (Check only if you're unmarried and pay more than half the costs of keeping up a home for yourself and a qualifying individual.)</label></li>
           <li class="gchoice_25_9_3"><input name="input_9" type="radio" value="Exempt" id="choice_25_9_3"><label for="choice_25_9_3" id="label_25_9_3">Exempt</label></li>
        </ul>
     </div>
  </li>
  
  <!-- Your hidden input -->
  <input type="text" class="gform_hidden" name="input_25_24_valid" id="input_25_24_valid">
</form>