drju37335 2016-03-03 00:46
浏览 119
已采纳

jQuery无法根据select选项显示/隐藏元素

I am building a wordpress widget that allows users to build a simple form in the customizer preview.

Part of my form builder is to have a dropdown select input with several form elements listed (text, textarea, select, checkbox...etc), if the user selects either select, checkbox or radio as a form element, then a div containing a textarea where the user can list the options for these elements is shown via jQuery.

Obviously, the user can add unlimited elements, so I dynamically assign an id to each dropdown and each container div for the options so that I can show/hide them for each element that is added by the user.

Here is my simplified html markup showing the essence of what I have. You will see the dropdown where user can select the form element, and also the div containing a text area. Because each dropdown relates to a textarea, I have added jQuery code above them both to show/hide the textarea container:

<script>
  jQuery(document).ready(function($) {
    var arr = ['select', 'checkbox', 'radio'];
    var thisForm = '#select-input-118 select.input_type';

    function showHideSelect() {
      var val = $(thisForm + ' option:selected').val();
      var selectOptions = $('#select-options-118')

      if (arr.indexOf(val) >= 0) {
        selectOptions.show();
      } else {
        selectOptions.hide();
      }
    }

    jQuery(document).ready(function($) {
      showHideSelect();

      $(thisForm).change(function() {
        showHideSelect();
      });
    });

  });

</script>
<p id="form_builder_input_type select-input-118" class="layers-form-item layers-column layers-span-9">
  <label for="widget-layers-widget-form_builder-3-input_type">Input Type</label>
  <select size="1" id="widget-layers-widget-form_builder-3-form_builders-118-input_type" name="widget-layers-widget-form_builder[3][form_builders][118][input_type]" placeholder="Select a form item" class="input_type">
    <option value="">Select a form item</option>
    <option value="text" selected="selected">
      Text </option>
    <option value="email">
      Email </option>
    <option value="textarea">
      Text Area </option>
    <option value="select">
      Drop Down Menu </option>
    <option value="checkbox">
      Checkbox </option>
    <option value="radio">
      Radio Buttons </option>
  </select>
</p>

<div id="select-options-118">
  <p id="select_options" class="layers-form-item">
    <label for="widget-layers-widget-form_builder-3-form_builders-118-select_options">Select Options</label>
  </p>
  <p style="background:#f5f5f5; padding:10px 20px; margin-bottom:20px;">One option per line</p>
  <textarea id="widget-layers-widget-form_builder-3-form_builders-118-select_options" name="widget-layers-widget-form_builder[3][form_builders][118][select_options]" placeholder="Add your options here" class="layers-text"></textarea>
  <p></p>
</div>

As the user can create unlimited number of form elements, the id's are dynamically inserted to the elements and into the jQuery code via php - I have just taken a snippet from the outputted html here. You can see a jsfiddle also here

The idea here being that for each dropdown, if the user selects either "select", "checkbox", or "radio". The div containing the options textarea shows, in all other cases it hides.

I have 2 questions: 1) My jQuery code isn't working and I can't see why 2) Is there a better way for me to code this, rather than have a snippet of jquery for every instance of dropdown/textarea? i.e. can I write a global piece of jQuery code that will add this function to all instances?

  • 写回答

1条回答 默认 最新

  • dongyi9298 2016-03-03 04:22
    关注

    The problem is your change event is on the form, not the element. If you set thisform to '#widget-layers-widget-form_builder-3-form_builders-118-input_type', it works.

    See https://jsfiddle.net/tghw/46stb05y/3/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀