根据其他字段清除选择字段选项

时间:2016-02-12 13:17:37

标签: javascript jquery forms

现场演示 https://jsfiddle.net/barrycorrigan/23hxgw6y/2/

所以我有一个表单,其中有一些隐藏的选择字段,只根据您从其他字段中选择的内容显示。哪个没有问题。

我的问题是表单被提交并且用户从其他字段中选择了几个不同的选项(因为它们只是隐藏)。它们仍然以提交的形式出现。

如果隐藏了其他字段中的选项,是否有办法清除? 另外我知道这个代码真的很脏,所以如果有人有任何关于使它更清洁的提示我会很感激你的建议。

$(document).ready(function() {

toggleFields(); //call this first so we start out with the correct visibility depending on the selected form values
//this will call our toggleFields function every time the selection value of our underAge field changes

$("#form1_segment").change(function() {
  toggleFields();
});

//this toggles the visibility of our parent permission fields depending on the current selected value of the underAge field
function toggleFields() {
  if ($("#form1_segment").val() == 'Food Service') {
    $("#form1_product").show();

    $("#form1_product_bc").hide();
    $("#form1_product_in").hide();
    $("#form1_product_hc").hide();
    $("#form1_product_auto").hide();
    $("#form1_product_aero").hide();

  } else if ($("#form1_segment").val() == 'Building Care') {
    $("#form1_product_bc").show();

    $("#form1_product").hide();
    $("#form1_product_in").hide();
    $("#form1_product_hc").hide();
    $("#form1_product_auto").hide();
    $("#form1_product_aero").hide();

  } else if ($("#form1_segment").val() == 'Industry') {
    $("#form1_product_in").show();

    $("#form1_product").hide();
    $("#form1_product_bc").hide();
    $("#form1_product_hc").hide();
    $("#form1_product_auto").hide();
    $("#form1_product_aero").hide();

  } else if ($("#form1_segment").val() == 'Health Care') {
    $("#form1_product_hc").show();

    $("#form1_product").hide();
    $("#form1_product_bc").hide();
    $("#form1_product_in").hide();
    $("#form1_product_auto").hide();
    $("#form1_product_aero").hide();

  } else if ($("#form1_segment").val() == 'Automotive') {
    $("#form1_product_auto").show();

    $("#form1_product").hide();
    $("#form1_product_bc").hide();
    $("#form1_product_in").hide();
    $("#form1_product_hc").hide();
    $("#form1_product_aero").hide();

  } else if ($("#form1_segment").val() == 'Aerospace') {
    $("#form1_product_aero").show();

    $("#form1_product").hide();
    $("#form1_product_bc").hide();
    $("#form1_product_in").hide();
    $("#form1_product_hc").hide();
    $("#form1_product_auto").hide();

  } else {
    $("#form1_product").hide();
    $("#form1_product_bc").hide();
    $("#form1_product_in").hide();
    $("#form1_product_hc").hide();
    $("#form1_product_auto").hide();
    $("#form1_product_aero").hide();
  }
}
});

1 个答案:

答案 0 :(得分:0)

您可以使用^启动选择器查找id所有以form1_product开头的选项,以隐藏所有选项并清除其值。然后switch显示正确的选择。



$(document).ready(function() {

  toggleFields();

  $("#form1_segment").change(function() {
    toggleFields();
  });

  function toggleFields() {

    $("[id^=form1_product]").hide().val(null);

    switch ($("#form1_segment").val()) {
      case "Food Service":
        $("#form1_product").show();
        break;
      case "Building Care":
        $("#form1_product_bc").show();
        break;
      case "Industry":
        $("#form1_product_in").show();
        break;
      case "Health Care":
        $("#form1_product_hc").show();
        break;
      case "Automotive":
        $("#form1_product_auto").show();
        break;
      case "Aerospace":
        $("#form1_product_aero").show();
    }
  }
});

div {
  display: block;
  margin-bottom: 20px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1_contact" class="contact-form" action="/en-us/contact" method="post">
  <div>
    <input id="form1_name" name="name" class="contact-form__input" placeholder="Full Name" type="text" required="required" />
  </div>
  <div>
    <input id="form1_email" name="email" class="contact-form__input" placeholder="Email address" type="email" required="required" />
  </div>
  <div class="split-form">
    <label for="form1_segment">Segment</label>
    <select id="form1_segment" name="segment" class="test">
      <option value="Select a Segment">Select a Segment</option>
      <option value="Food Service">Food Service</option>
      <option value="Building Care">Building Care</option>
      <option value="Industry">Industry</option>
      <option value="Health Care">Health Care</option>
      <option value="Automotive">Automotive</option>
      <option value="Aerospace">Aerospace</option>
    </select>
  </div>
  <div class="split-form last">
    <label for="form1_product">Product</label>
    <select id="form1_product" name="product" class="test">
      <option value="Select a product">Select a product</option>
      <option value="S.U.D.S.® Single Use Dispensing System">S.U.D.S.® Single Use Dispensing System</option>
      <option value="Chix® SC Foodservice Towels">Chix® SC Foodservice Towels</option>
      <option value="Chix® SC Foodservice Towels">Chix® SC Foodservice Towels</option>
      <option value="Chix® Pro-Quat®">Chix® Pro-Quat®</option>
      <option value="Chix® Pro-Quat®">Chix® Pro-Quat®</option>
      <option value="Chix® Pro-Quat®">Chix® Pro-Quat®</option>
      <option value="Chix® Pro-Chlor®">Chix® Pro-Chlor®</option>
      <option value="Chix® Microfiber Light Cloths">Chix® Microfiber Light Cloths</option>
      <option value="Chix® Plus with Microban">Chix® Plus with Microban</option>
      <option value="Chix® Towels with Microban">Chix® Towels with Microban</option>
      <option value="Chix® Towels">Chix® Towels</option>
      <option value="Chix® Plus Sanitizing">Chix® Plus Sanitizing</option>
      <option value="Chix® Soft Cloth">Chix® Soft Cloth</option>
      <option value="Chix® Tough">Chix® Tough</option>
      <option value="Chix® All Day™">Chix® All Day™</option>
      <option value="Chix® Wet Wipes">Chix® Wet Wipes</option>
      <option value="Chix® Competitive Wet Wipes">Chix® Competitive Wet Wipes</option>
      <option value="Chix® Economy Towels">Chix® Economy Towels</option>
      <option value="Chix® 3000">Chix® 3000</option>
      <option value="Quix® Pretreated Towels">Quix® Pretreated Towels</option>
      <option value="Chix® Apron">Chix® Apron</option>
      <option value="Worxwell®">Worxwell®</option>
    </select>
    <select id="form1_product_bc" name="product_bc" class="test">
      <option value="Select a product">Select a product</option>
      <option value="Dusty™ High Performance Dust Cloth">Dusty™ High Performance Dust Cloth</option>
      <option value="Masslinn® Heavy Duty Dust Cloth">Masslinn® Heavy Duty Dust Cloth</option>
      <option value="S.U.D.S.® Single Use Dispensing System">S.U.D.S.® Single Use Dispensing System</option>
      <option value="Masslinn® Medium Duty Dust Cloth">Masslinn® Medium Duty Dust Cloth</option>
      <option value="Masslinn® Light Duty Dust Cloth">Masslinn® Light Duty Dust Cloth</option>
      <option value="Economy Duster">Economy Duster</option>
      <option value="Masslinn Dusting Tools">Masslinn Dusting Tools</option>
    </select>
    <select id="form1_product_in" name="product_in" class="test">
      <option value="Select a product">Select a product</option>
      <option value="Durawipe® Heavy Duty Shop Towel">Durawipe® Heavy Duty Shop Towel</option>
    </select>
    <select id="form1_product_hc" name="product_hc" class="test">
      <option value="Select a product">Select a product</option>
      <option value="Worxwell® Towels">Worxwell® Towels</option>
      <option value="Soft Cloth Patient Wipes">Soft Cloth Patient Wipes</option>
      <option value="Professional Series">Professional Series</option>
      <option value="Dwell™ Disinfectant Compatible Healthcare Towels">Dwell™ Disinfectant Compatible Healthcare Towels</option>
      <option value="Dusty™ High Performance Dust Cloths">Dusty™ High Performance Dust Cloths</option>
    </select>
    <select id="form1_product_auto" name="product_auto" class="test">
      <option value="No products available">No products available</option>
    </select>
    <select id="form1_product_aero" name="product_aero" class="test">
      <option value="No products available">No products available</option>
    </select>
  </div>
  <div>
    <textarea id="form1_message" name="message" class="contact-form__input  contact-form__textarea" cols="30" rows="4" placeholder="Message"></textarea>
  </div>
  <div>
    <input id="form1_submit" name="submit" class="btn  btn__round  subform__button" value="Send Message" type="submit" />
  </div>
</form>
&#13;
&#13;
&#13;