[英]Show element when the user starts typing
使用JavaScript可以嗎? 現在,我用它來顯示TextArea聚焦時的元素。 但是,僅當它已聚焦並且在TextArea中輸入了內容時,我才能顯示它嗎?
<form action="#" method="post">
<textarea onfocus="document.getElementById('submit').style.display = 'block';" id="text" style="width: 540px; height: 50px; overflow:hidden;"></textarea>
<input id="submit" type="submit" value="Submit" style="display:hidden">
</form>
嘗試了SpenserJ的代碼,但它無法正常工作。 提交按鈕將不會顯示。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style type="text/css">
#text {
width: 540px;
height: 50px;
overflow: hidden;
}
#submit {
display: none;
}
</style>
<script>
var textInput = document.getElementById('text')
, submitButton = document.getElementById('submit');
function checkTextValue() {
if (textInput.value !== '') {
submitButton.style.display = 'block';
} else {
submitButton.style.display = 'none';
}
}
</script>
</head>
<body>
<form action="#" method="post">
<textarea onkeypress="checkTextValue()" onkeyup="checkTextValue()" onchange="checkTextValue()" id="text"></textarea>
<input id="submit" type="submit" value="Submit">
</form>
</body>
</html>
使用onkeypress,onkeyup和onchange來檢測值的變化,然后根據顯示是否為空來設置顯示。
http://codepen.io/SpenserJ/pen/xKmcs
HTML:
<form action="#" method="post">
<textarea onkeypress="checkTextValue()" onkeyup="checkTextValue()" onchange="checkTextValue()" id="text"></textarea>
<input id="submit" type="submit" value="Submit">
</form>
JS:
var textInput = document.getElementById('text')
, submitButton = document.getElementById('submit');
function checkTextValue() {
if (textInput.value !== '') {
submitButton.style.display = 'block';
} else {
submitButton.style.display = 'none';
}
}
CSS:
#text {
width: 540px;
height: 50px;
overflow: hidden;
}
#submit {
display: none;
}
對不起,我之前的onblure
響應是正確的版本,它使用onblure
而不是onfocus
,也可以使用keyup,keydown,keypress
..
function checkFortextAreaValue(textArea){
if(textArea.value.length > 0){
document.getElementById('submit').style.display = 'block';
}else{
alert('textArea is empty');
}
}
<form action="#" method="post">
<textarea id="text" style="width: 540px; height: 50px; overflow:hidden;" onblur="checkFortextAreaValue(this); "></textarea>
<input id="submit" type="submit" value="Submit" style="display:hidden" />
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.