gpt4 book ai didi

javascript - 使用 JavaScript 更改输入边框颜色不起作用

转载 作者:行者123 更新时间:2023-11-28 14:15:20 24 4
gpt4 key购买 nike

我已经阅读了一些关于 SO 的问题,我相信我做的是正确的,但它仍然不起作用!

我正在使用普通的旧 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() 的原因 并尝试更改颜色。

有什么想法吗?

最佳答案

我猜它正在改变边框颜色,但是因为元素没有边框所以没有应用它试试:

 document.getElementById('Forename').style.border = "1px solid #ff0000";

关于javascript - 使用 JavaScript 更改输入边框颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9096686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com