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/

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

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程