【问题标题】:Background color of Text-box文本框的背景颜色
【发布时间】:2014-07-09 09:42:58
【问题描述】:

我希望文本框中的背景颜色在单击提交按钮后至少保持 5 秒

但是一旦我点击提交按钮它就会消失。

这是我的代码...

<!DOCTYPE>
<html>
<head>
<title>Text Boxes With Aplhabets </title>
</head>
<body>`enter code here`

<script type="text/javascript">

 frm.alphabets.value="";
function AllowAlphabets(){
               if (!frm.alphabets.value.match(/^[a-zA-Z]+$/) )
               {

                    frm.alphabets.focus(); 
                    frm.alphabets.style.background="Red";
                    //alert("Please Enter only alphabets");
               }

}      
</script>

<form name="frm"   onsubmit="return AllowAlphabets()">
Enter Aplhabets: <input type="text" name="alphabets" />
<input type = "submit" value = "Submit">
</form>
<br;>
</body>
</html>

【问题讨论】:

  • 我想你想要这样的东西jsfiddle.net/G6wa9/1
  • 当你评论警告框语句时,它没有给出预期的结果
  • 这就是为什么我说我认为,其实我不明白你想要的行为,只是将背景设置为红色没有任何关于错误的信息都会让用户感到困惑,至少在这种情况下我们需要一个工具提示。
  • 如果我输入除 aplhabets 以外的任何内容并单击提交按钮,文本框的背景颜色将发生变化,但背景颜色会瞬间消失。我需要背景停留一段时间

标签: javascript html css forms


【解决方案1】:

使用setTimeout()

例子:

setTimeout(myFunction, 2000);

或

setTimeout(function () {

//code here
}, 2000);

参考:MDN

【讨论】:

  • 重点不只是setTimeout(),OP 的代码在这里有一些(不仅仅是1)错误。首先测试有效字符串的方法不对,我们必须使用RegExp对象的test()方法(不是字符串对象的match()方法),其次我们必须return false或e.preventDefault()取消@ 987654329@.
  • @KingKing 但是你为什么不告诉 OP 而不是评论答案呢?您还应该提到 DOCTYPE 不好。
猜你喜欢
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 2013-01-15
相关资源
最近更新 更多