[英]How to Display Select Tag using 'change' jquery 2.1.4?
I am trying to display select box when user input some values in the textbox , I tried using onchange property of jquery but its not working. 当用户在文本框中输入一些值时,我试图显示选择框,我尝试使用jquery的onchange属性,但无法正常工作。
Below is my code 下面是我的代码
$(document).ready(function() { $("#total_enter").change(function() { $(".gst_sel_wrap").css("display", "block"); }); })
.gst_sel_wrap { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrap_box"> <input type="text" name="total_enter" id="total_enter"> <select class="gst_sel_wrap"> <option>GST SLABS</option> <option value="">0%</option> <option value="">5%</option> <option value="">12%</option> <option value="">18%</option> <option value="">28%</option> </select> </div>
Try using the jQuery keydown
method: https://api.jquery.com/keydown/ 尝试使用jQuery
keydown
方法: https : //api.jquery.com/keydown/
I also added some code to hide the dropdown in the beginning, so the effect becomes obvious. 我还添加了一些代码以在开始时隐藏下拉菜单,因此效果显而易见。
$(document).ready(function(){ $(".gst_sel_wrap").hide(); $("#total_enter").keydown(function(){ $(".gst_sel_wrap").show(); }); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrap_box"> <input type="text" name="total_enter" id="total_enter"> <select class="gst_sel_wrap"> <option>GST SLABS</option> <option value="">0%</option> <option value="">5%</option> <option value="">12%</option> <option value="">18%</option> <option value="">28%</option> </select> </div>
You can use either keyup
or keydown
or both as I do here: 您可以像在这里一样使用
keyup
或keydown
或两者都使用:
$(document).ready(function() { $("#total_enter").on("keyup keydown",function() { $(".gst_sel_wrap").css("display", "block"); }); })
.gst_sel_wrap { display:none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrap_box"> <input type="text" name="total_enter" id="total_enter"> <select class="gst_sel_wrap"> <option>GST SLABS</option> <option value="">0%</option> <option value="">5%</option> <option value="">12%</option> <option value="">18%</option> <option value="">28%</option> </select> </div>
To achieve this you can use the input
event, as it fires when a key is pressed and also when a value is copy/pasted in. You should also check the value of the element to ensure that it isn't empty, as the value can be deleted, when I would assume your select should be hidden again. 为此,您可以使用
input
事件,因为input
事件会在按下键以及复制/粘贴值时触发。您还应该检查元素的值以确保其不为空,因为该值当我假设您的选择应该再次隐藏时,可以将其删除。
Finally, note that you can use toggle()
to hide or show the select based on the given value, in a more succinct way. 最后,请注意,您可以使用
toggle()
以更简洁的方式隐藏或显示基于给定值的选择。 Try this: 尝试这个:
$(document).ready(function() { $("#total_enter").on('input', function() { $(".gst_sel_wrap").toggle(this.value.trim() != ''); }); })
.gst_sel_wrap { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrap_box"> <input type="text" name="total_enter" id="total_enter"> <select class="gst_sel_wrap"> <option>GST SLABS</option> <option value="">0%</option> <option value="">5%</option> <option value="">12%</option> <option value="">18%</option> <option value="">28%</option> </select> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.