如果选择了某些选项值,则显示字段-必须在页面加载时起作用 [英] Show field if certain option value is selected - must work on pageload

查看:78
本文介绍了如果选择了某些选项值,则显示字段-必须在页面加载时起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

天才代码适用于复选框:

$(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.

我正在尝试为选择框进行修改.代码由wordpress上的cforms自动生成.

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>

这是我尝试过的:

  $('#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?

谢谢!

推荐答案

您快到了,这将是一种更短的方法:

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();

您可以在此处尝试.

获取或设置<select>(或任何其他输入类型元素)的值时,可以使用 .val() .如果有人想要这个没有动画,它看起来像这样:

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();

这篇关于如果选择了某些选项值,则显示字段-必须在页面加载时起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆