繁体   English   中英

从 dropdwon 中选择时,为显示/隐藏文本框提供必填字段

[英]Give required field for show/hide textbox when selected from the dropdwon

我必须提供显示/隐藏文本框必填字段.. 当用户从下拉文本框中选择其他时,将显示.. 我必须为该文本框提供必填字段.. 这是代码

 $("[name='mass']").change(function(){ if($(this).val() == "Other" ) { $('.other').slideDown(); $('.other').slideDown().find('.other').attr('required', true); } else { $('.other').slideUp(); $('.other').slideDown().find('.other').attr('required', false); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="mass" id="types" class="col-md-12" required="required"> <option value="">Mass</option> <option value="Soul of">Soul of</option> <option value="Other">Others</option> </select> <div class="other" style="display:none;" > <label style="text-align:center;">If Others</label> <input type="text" name="other" id="other" placeholder="If Others" maxlength="30" class="col-md-12"></div>

在你的.find('.other')你正在寻找一个类,但输入有id="other"而不是class="other"

同样在您的else语句中,您正在使用slideUpslideDown进行滑动,因此它将始终显示。

 $("[name='mass']").change(function() { if ($(this).val() == "Other") { $('.other').slideDown().find('#other').attr('required', true); } else { $('.other').slideUp().find('#other').attr('required', false); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="mass" id="types" class="col-md-12" required="required"> <option value="">Mass</option> <option value="Soul of">Soul of</option> <option value="Other">Others</option> </select> <div class="other" style="display:none;"> <label style="text-align:center;">If Others</label> <input type="text" name="other" id="other" placeholder="If Others" maxlength="30" class="col-md-12"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM