简体   繁体   English

跨度/隐藏输入(如果选择的值是SMTH)

[英]Span / Hide input if selected Value is SMTH

I have select with options and I have many inputs. 我选择了选项,并且有很多输入。 I need to span ready input if I select in option for example "USER" and hide it again if select another option. 如果我在“ USER”等选项中选择,我就需要准备好输入,如果选择另一个选项,则需要再次隐藏它。

 <form class="form-signup" action="admin_users.php" method="POST" id="payment-form"> <div class="well well-sm"> <h3 class="form-signin-heading"> Create <select id="selectlist" name="selectproduct" > <option value="1">User</option> <option value="2">Admin</option> <option value="3">Smth else</option> <option value="4">Smth else of else</option> </select> </h3> <div class="form-group"> <div class="col-xs-12"> <input class="form-control" type="text" name="username" id="username" placeholder="İstifadəçi" value="first input" required autofocus> </div> <div> &nbsp </div> <div class="col-xs-12"> <input type="text" class="form-control" id="fname" name="fname" placeholder="Ad" value="second input" required> </div> <div> &nbsp </div> <input class='btn btn-primary' type='submit' name='addUser' value='Create user' /> </div> </form> 

 $( "#selectlist" ).change(function(event) { if (this.value == 1 || this.value == 2) { $(".form-control").show(); } else { $(".form-control").hide(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="form-signup" action="admin_users.php" method="POST" id="payment-form"> <div class="well well-sm"> <h3 class="form-signin-heading"> Create <select id="selectlist" name="selectproduct" > <option value="1">User</option> <option value="2">Admin</option> <option value="3">Smth else</option> <option value="4">Smth else of else</option> </select> </h3> <div class="form-group"> <div class="col-xs-12"> <input class="form-control" type="text" name="username" id="username" placeholder="İstifadəçi" value="first input" required autofocus> </div> <div> &nbsp </div> <div class="col-xs-12"> <input type="text" class="form-control" id="fname" name="fname" placeholder="Ad" value="second input" required> </div> <div> &nbsp </div> <input class='btn btn-primary' type='submit' name='addUser' value='Create user' /> </div> </form> 

I would use a custom attribute called valueTie . 我将使用一个名为valueTie的自定义属性。 When you update the select, your content will automatically update accordingly. 更新选择时,您的内容将相应地自动更新。 What's nice is all you have to do is add another option and another div with the attribute valueTie and you're up and running with another option. 所要做的就是添加另一个选项和另一个带有valueTie属性的div,然后您就可以使用另一个选项来运行。

 $("#selectlist").change(function () { $("[valueTie]").hide() $("[valueTie='" + $(this).val() + "']").show(); }); $("#selectlist").change(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="form-signup" action="admin_users.php" method="POST" id="payment-form"> <div class="well well-sm"> <h3 class="form-signin-heading"> Create <select id="selectlist" name="selectproduct" > <option value="1">User</option> <option value="2">Admin</option> </select> </h3> <div class="form-group"> <div class="col-xs-12" valueTie="1"> <input class="form-control" type="text" name="username" id="username" placeholder="İstifadəçi" value="first input" required autofocus> </div> <div class="col-xs-12" valueTie="2"> <input type="text" class="form-control" id="fname" name="fname" placeholder="Ad" value="second input" required> </div> <div> &nbsp </div> <input class='btn btn-primary' type='submit' name='addUser' value='Create user' /> </div> </form> 

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

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