【问题标题】:Display conditional text in paragraph depending on chosen value dropdown cf7根据所选值下拉 cf7 在段落中显示条件文本
【发布时间】:2021-05-11 09:24:36
【问题描述】:

基于this 线程我有一个下拉菜单,它在表单的下一个问题的句子中显示所选值。 Now I would also like, when one of the values is chosen, another question of the form shows some other text in the sentence, like this:

年份是下拉值,金额是问题(段落)中应显示的条件文本。

2020:30.846,- 2019:30.360,- 2018:30.000,- 2017:25.000,- 2016 年:24.437,-

我已经学会了如何使用值来管理它,而不是根据所选值使用不同的文本。

代码我现在要得到的年数:

<p>Did you unsubscribed yourself in <span id="recent-years"></span>?</p>
[radio radio-uitgeschreven-nl class:inline-control "Yes" "No"]

并获得岁月:

<script>
jQuery(document).ready(function( $ ){
$('select[name="recent-years"]').on('change', function(){
   updateParagraph(); 
});
updateParagraph();

function updateParagraph(){
// Assign variable $placeholder to the chosen value
   let $placeholder = $('select[name="recent-years"]').val();
   // replace 'your-field-name' with the cf7 field name
   $('input[name="ba-maanden"]').attr('placeholder', $placeholder);
   //New code to also add to the form html
   $('#recent-years').html($placeholder);
 }
});
</script>

输入字段 ba-maanden 用于其他内容。所以这就是为什么你也会在这里看到这个。

所以我希望当有人选择特定年份时,他们会在这个问题中看到上面提到的另一个金额:

<p>Do you have endowment insurance or savings account of more than 30.846 EUR?</p>
[radio radio-kapitaal-spaar class:inline-control "Ja" "Nee"]

我想我可以将金额放在&lt;span&gt;year-amount&lt;/span&gt; 之间。但是如何根据所选年份提取金额?

任何帮助将不胜感激!

最好的问候,

瓦斯科

【问题讨论】:

  • 你的代码示例在哪里?
  • 我编辑了原始帖子并添加了代码。谢谢。
  • 有人吗?我被卡住了...... :-(

标签: php html jquery wordpress contact-form-7


【解决方案1】:

像这样?

jQuery(document).ready(function($){
  $('select[name="recent-years"]').on('change', function(){
     changeQuestion($(this)); 
  });
  $('input[name="confirm-insurance"]').on('change', function(){
     changeQuestion($(this)); 
  });
});

function changeQuestion(object, selector){
  let currentParagraph = $(object).closest('.question');
  let currentValue = $(object).val();
  let nextParagraph = $(object).closest('.question').next('.question');
  
  $(currentParagraph).fadeOut("slow", function() {
    $(nextParagraph).show().removeClass('hide');
    $(nextParagraph).find('.lastAnswer').text(currentValue);
  });
}
.hide {
  display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="question">
  Did you unsubscribed yourself in 
  <span id="recent-years">
   <select name="recent-years">
     <option selected="true" disabled="disabled">Select year</option>
     <option value="30.846">2020</option>
     <option value="30.360">2019</option>
   </select>
  </span> ?
</p>

<p class="question hide">
  Do you have endowment insurance or savings account of more than <span class="lastAnswer"></span> ?<br>Yes <input type="radio" name="confirm-insurance" value="yes"> No <input type="radio" name="confirm-insurance" value="no">
</p>

<p class="question hide">
  Your answer is <span class="lastAnswer"></span>.
</p>

【讨论】:

  • 谢谢,但这不是我的意思。现在它的工作原理是这样的:当有人从下拉菜单中选择一个 ID 为 recent-years 的选项时,该选项将显示在本段中:&lt;p&gt;Did you unsubscribed yourself in &lt;span id="recent-years"&gt;&lt;/span&gt;?&lt;/p&gt;。根据所选选项,我想在新问题中获得另一个值。示例:如果有人选择答案 2021,他们将看到 &lt;p&gt;Do you have endowment insurance or savings account of more than &lt;span id="amount"&gt;&lt;/span&gt;?&lt;/p&gt; 其中 amount 是可变的,具体取决于 recent-years 下拉列表中选择的选项
  • @Vasco Check plz :)
  • 这对我不起作用 :-( 但我是否纠正了这段代码不适合 cf7?因为那不是我正在使用的。谢谢。
  • 我在演示中看到它运行良好!但是在我的网站上我看到了这个,请看这个screenshot。您可以看到有一个额外的选择输入字段,这不是必需的,并且在最后一个问题中看不到金额。在此屏幕截图中,您会看到第一个下拉列表中有一个年份日期。那一年显示在下一个问题中(不是现在,通常在“in”和“ingeschreven”之间。然后根据该年份的日期值,在最后一个问题中必须有一个可见的金额。抱歉给您带来不便,谢谢对我帮助很大。
  • 因为你是用编辑网站制作的......
猜你喜欢
  • 2017-03-30
  • 2021-07-01
  • 2015-03-08
  • 1970-01-01
  • 2015-05-26
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多