输入字段值重新填充如果焦点后空
问题描述:
所以我正在一个窗体上工作。当您聚焦(单击)文本字段时,该值将变为空白。现在我想要默认值重新出现,如果用户没有填写任何东西。输入字段值重新填充如果焦点后空
因此,例如我有一个ID为Question1的文本字段。这是明确的重点。
$(document).ready(function() {
$("#Question1").val('Naam *');
$("#Question1").focus(function() {
$("#Question1").val('');
});
});
任何JavaScript的代码?
编辑:由于CMS限制(concrete5),我无法使用占位符。文本字段被生成。
答
你可以做这样的事情与blur()
事件
$(document).ready(function() {
var temp = 'Naam *';
$("#Question1").focus(function() {
if (temp == this.value)
// ------^-- empty the field only if the field value is initial
this.value = '';
// emptying field value
}).blur(function() {
if (this.value.trim() == '')
// -----^-- condition field value is empty
this.value = temp;
// if empty then updating with initial value
}).val(temp);
//----^-- setting initial value
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="Question1" />
你为什么不使用占位符的帮助? –
使用placeholder =“something”,如果没有写在它上面,它会重新出现 –
http://google.com/?q=jquery+placeholder+plugin – Andreas