[英]How can show a div when input has a value?
I have an input text that and a hide div. 我有一个输入文本和一个隐藏div。
I want to show the div when the input text has a value. 当输入文本具有值时,我想显示div。
Here is the demo: http://jsfiddle.net/vyc7N/181/ 这是演示: http : //jsfiddle.net/vyc7N/181/
<label for="db">Type whatever</label>
<input type="text"name="amount" />
<div id="yeah" style="display:none;">
<input type="submit" />
</div>
Please somebody can help me? 请有人可以帮我吗?
You can write the code in keyup event of textbox to get the length of text entered in it. 您可以在文本框的keyup事件中编写代码,以获取在其中输入的文本的长度。
$('input[name=amount]').keyup(function(){
if($(this).val().length)
$('#yeah').show();
else
$('#yeah').hide();
});
you can also use .toggle()
instead of using .show
/ .hide()
: 您还可以使用.toggle()
而不是.show
/ .show
.hide()
:
$('input[name=amount]').keyup(function(){
$('#yeah').toggle($(this).val().length);
});
$('input[name=amount]').bind('keyup change', function(){
if(!$(this).val())
$("#yeah").css('display', 'none');
else
$("#yeah").css('display', '');
});
You could use onkeyup()
: 您可以使用onkeyup()
:
<label>Type whatever</label>
<input id='inptOne' type="text" name="amount" onkeyup="myFunction()" />
<div id="yeah" style="display:none">
<input type="submit" />
</div>
<script>
//if input has some value will show the button
window.myFunction = function() {
//alert('it ran!');
var hasValue = document.getElementById('inptOne').value;
if (!!hasValue) {
document.getElementById('yeah').style.display = 'inline';
} else {
document.getElementById('yeah').style.display = 'none';
};
};
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.