如果焦点为空,则输入字段值重新填充
Input field value refill if empty after on focus
所以我正在制作表格。当您聚焦(单击)文本字段时,该值变为空白。现在我希望在用户未填写任何内容时重新显示默认值。
例如,我有一个 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" />
所以我正在制作表格。当您聚焦(单击)文本字段时,该值变为空白。现在我希望在用户未填写任何内容时重新显示默认值。
例如,我有一个 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" />