我已阅读了几个有关此问题的问题,我相信我正在做正确的事情,但仍然无法正常工作!使用JavaScript无法更改输入边框颜色
我使用普通的旧JavaScript作为遗留代码,但如果必须我也可以使用jQuery。
我有我的形式,其为形式的(没有双关语意):
<form action="submit.php" method="post" name="myForm" onsubmit="return validateInfoForm()">
<input type="text" class="boxes" name="Forename" id="Forename" placeholder="Forename" />
....
</form>
JS
function validateInfoForm()
{
var b=document.forms["myForm"]["Forename"].value;
var c=document.forms["myForm"]["Surname"].value;
var f=document.forms["myForm"]["Postcode"].value;
var g=document.forms["myForm"]["Telno"].value;
var h=document.forms["myForm"]["Email"].value;
if (b==null || b=="")
{
alert("Enter Forename");
document.getElementById('Forename').style.borderColor = "#FF0000";
// before it was using document.forms["myForm"]["Forename"] as the selector but I changed it to try and get it to work. It was also doing .focus() rather than changing the border colour.
return false;
}
没有收到任何错误,我只是得到了警告框,然后我的光标被放置在输入内,我也不想要,因为它是删除占位符,这就是为什么我已经删除了.focus()
并试图改变颜色。
任何想法?
感谢,尝试过,但结果是一样的。在所有文本输入元素上已经有一个蓝色的2px边框,所以我认为它应该变成红色,但即使添加宽度似乎也不会影响它。 – martincarlin87 2012-02-01 13:48:29
WFM。 jquery-ish中的简化版本http://jsfiddle.net/6pN2q/ – Yule 2012-02-01 13:57:46
嗯,谢谢你。我现在完全困惑为什么我的代码不工作:( – martincarlin87 2012-02-01 14:02:15