我有一个表单,其中预先填充了可用性/提示的值 - 不幸的是,其中一个字段针对我无权访问的 servlet 进行了验证。
所以当我使用时:
<input type="text" class="promocode" name="LLN" id="promo1" value="Enter promotion code" maxlength="40" title="Promotion Code 1" onfocus="this.value=''; this.onfocus=null;">
这不起作用,因为验证 Servlet 发现该字段已被填充,然后它通过验证过程运行它并返回错误(因为它不是正确的格式)。
我知道如果我有一些 js 可以使用,我可以让它只针对值而不是默认值运行验证。
但在这种情况下,我不确定如何在不填写值的情况下显示表单字段提示。
想法?
最佳答案
一个快速的解决方案是添加 <span>
位于您的输入之上,并在输入获得焦点时将其隐藏。这甚至不会触及输入。
您可以使用相对/绝对来定位跨度。
我希望你喜欢这个解决方案
HTML
<div class="inputContainer">
<input type="text" id="theInput" />
<span id="theSpan">Please fill this input!</span>
</div>
CSS:
.inputContainer{position:relative;}
#theSpan{position:absolute; top:0; left:0}
JS
$('#theInput').focus(function(){
$('#theSPan').hide()
});
$('#theInput').blur(function(){
if($(this).val()==''){ $('#theSPan').show()}
});
关于javascript - 字段值(用于提示/可用性)显示而不使其成为字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6710222/