如果选择特定选项值则显示字段 - 必须在页面加载时工作

发布于 2024-09-16 08:28:58 字数 1225 浏览 2 评论 0原文

这个天才代码适用于复选框:

$(document).ready(function() {
   $("#Languages-spoken-and-understood-8").change(function() {
      $("#li-2-21")[$(this).is(":checked") ? 'show' : 'hide']("fast")
  }).change();
});

我喜欢的是它可以在负载上工作。

我试图将其修改为选择框。代码是由 WordPress 上的 cforms 自动生成的。

<select name="Severity-of-your-symptoms" id="Severity-of-your-symptoms" class="cformselect" >
    <option value="full01_severity_notselected" selected="selected">Please select...</option>
    <option value="Mild">Mild</option>
    <option value="Moderate">Moderate</option>
    <option value="Severe">Severe</option>
    <option value="full01_severity_other">Other</option>
</select>

这就是我尝试过的:

  $('#Severity-of-your-symptoms').change(function() {
    $("#li-10-14")[("#Severity-of-your-symptoms[value='full01_severity_other']").attr('selected', 'selected') ? 'show' : 'hide']("fast")
  });change();

显然它不起作用。仅当选择“其他”时,才会出现文本框。 有没有办法以某种方式再次使用那个漂亮的代码,或者我是否需要更大的函数?

谢谢!

This genius code works fine for checkboxes:

$(document).ready(function() {
   $("#Languages-spoken-and-understood-8").change(function() {
      $("#li-2-21")[$(this).is(":checked") ? 'show' : 'hide']("fast")
  }).change();
});

What I love about is that it works onload.

I was trying to modify it for a select box. Code is autogenerated by cforms on wordpress.

<select name="Severity-of-your-symptoms" id="Severity-of-your-symptoms" class="cformselect" >
    <option value="full01_severity_notselected" selected="selected">Please select...</option>
    <option value="Mild">Mild</option>
    <option value="Moderate">Moderate</option>
    <option value="Severe">Severe</option>
    <option value="full01_severity_other">Other</option>
</select>

This is what I tried:

  $('#Severity-of-your-symptoms').change(function() {
    $("#li-10-14")[("#Severity-of-your-symptoms[value='full01_severity_other']").attr('selected', 'selected') ? 'show' : 'hide']("fast")
  });change();

Obviously it's not working. Only if "Other" is selected, the text box should appear.
Is there a way to use that nifty code again somehow or do I need a bigger function?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅刃 2024-09-23 08:28:58

你就快到了,这将是一个更短的方法:

$('#Severity-of-your-symptoms').change(function() {
  $("#li-10-14")[$(this).val() == "full01_severity_other" ? 'show' : 'hide']("fast");
}).change();

你可以在这里尝试一下< /a>.

获取或设置

$('#Severity-of-your-symptoms').change(function() {
  $("#li-10-14").toggle($(this).val() == "full01_severity_other");
}).change();

You're almost there, this would be a much shorter way of doing it:

$('#Severity-of-your-symptoms').change(function() {
  $("#li-10-14")[$(this).val() == "full01_severity_other" ? 'show' : 'hide']("fast");
}).change();

You can give it a try here.

When getting or setting the value of a <select> (or any other input type element) you can use .val(). In case anyone wants this without the animation, it'd look like this:

$('#Severity-of-your-symptoms').change(function() {
  $("#li-10-14").toggle($(this).val() == "full01_severity_other");
}).change();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文