gpt4 book ai didi

javascript - 清除基于另一个字段的选择字段选项

转载 作者:行者123 更新时间:2023-11-28 06:22:52 26 4
gpt4 key购买 nike

现场演示 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();
}
}
});

最佳答案

您可以使用^开头选择器来查找所有idform1_product开头的选择,以隐藏它们并清除它们的值。然后切换以显示正确的选择。

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

关于javascript - 清除基于另一个字段的选择字段选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35363611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com