Firefox 4:有没有办法删除所需表单输入中的红色边框?

发布于 2024-11-05 23:54:03 字数 533 浏览 3 评论 0原文

当在表单字段中定义 required 时,Firefox 4 会自动显示该元素的红色边框,甚至在用户点击提交按钮之前也是如此。

<input type="text" name="example" value="This is an example" required />

我认为这对用户来说是令人不安的,因为他/她一开始就没有犯错误。

我想隐藏初始状态的红色边框,但如果缺少标记为必填的字段,则当用户点击发送按钮时显示它。

我从新的伪选择器中查看了 :required:invalid ,但更改是在验证之前和之后进行的。 (来自 Firefox 4 必需的输入表单红色边框/轮廓

有没有办法在用户提交表单之前禁用红色边框,并在缺少某些字段时显示它?

When required is defined in a form field, Firefox 4 automatically shows a red border to this element, even BEFORE the user hits the submit button.

<input type="text" name="example" value="This is an example" required />

I think this is disturbing for the user as he/she asn't made mistakes at the beginning.

I wnat to hide that red border for the initial state, but show it when the user hits the send button if there is a missing field marked as required.

I looked at :required and :invalid from new pseudo selector, but the changes are for before AND after the validation. (from Firefox 4 Required input form RED border/outline)

Is there a way to disable the red border before the user submits the form, and show it if there is some missing fields ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

权谋诡计 2024-11-12 23:54:03

这有点棘手,但我已经设置了这个例子: http://jsfiddle.net/c5aTe/ 这对我有用。基本上,这个技巧似乎是绕过无效的占位符文本。否则你应该能够这样做:

input:required {
    box-shadow:none;
}
input:invalid {
    box-shadow:0 0 3px red;
}

或类似的东西...

但是因为FF4决定验证你的占位符文本(不知道为什么...)小提琴中的解决方案(小黑客 - 使用!important) 是必需的。

编辑

Doh!! - 我觉得很傻。我已经更新了我的小提琴: http://jsfiddle.net/c5aTe/2/ - 你可以使用 :focus 伪类来使元素在用户键入时保持有效样式。如果项目失去焦点时内容无效,这仍然会以红色突出显示,但我认为您对设计的行为只能做这么多...

HTH :)


接受后编辑:应

OP要求的示例摘要(注意前两个仅设计用于 FF4 - 不是 Chrome

  1. 修复 FF 验证占位符文本的问题:http://jsfiddle.net/c5aTe/
  2. 修复输入时进行 FF 验证的问题:http ://jsfiddle.net/c5aTe/2
  3. JS解决方案切换样式/验证: http://jsfiddle.net/c5aTe/4

This was a little tricky but I've set up this exmaple: http://jsfiddle.net/c5aTe/ which is working for me. Basically the trick seems to be getting around having placeholder text which is invalid. Otherwise you should be able do this:

input:required {
    box-shadow:none;
}
input:invalid {
    box-shadow:0 0 3px red;
}

or something similar...

BUT since FF4 decides to validate your placeholder text (no idea why...) the solution in the fiddle (little hacky - uses !important) is required.

EDIT

Doh!! - I feel well silly. I've updated my fiddle: http://jsfiddle.net/c5aTe/2/ - you can use the :focus pseudo class to keep the element styled as if valid while the user is typing. This will still highlight in red if the content is invalid when the item loses focus but I think there is only so much you can do with the designed behaviour...

HTH :)


EDIT after acceptance:

Summary of examples at OP's request (note the first two are only designed for FF4 - not Chrome)

  1. Fix for FF validating your place holder text: http://jsfiddle.net/c5aTe/
  2. Fix for FF validating as you type: http://jsfiddle.net/c5aTe/2
  3. JS solution toggling styles/validation: http://jsfiddle.net/c5aTe/4
温馨耳语 2024-11-12 23:54:03

从 Firefox 26 开始,用于识别无效必填字段的实际 CSS 如下(来自 forms.css):

:not(output):-moz-ui-invalid {
    box-shadow: 0 0 1.5px 1px red;
}

为了在其他浏览器中复制,我使用:

input:invalid {
    box-shadow: 0 0 1.5px 1px red;
}

我使用了像素设置,但我永远不会猜到 1.5px无需查看moz源代码。

要禁用它,您可以使用:

input:invalid {
    box-shadow: none;
}

As of Firefox 26, the actual CSS used to identify invalid required fields is as follows (comes from forms.css):

:not(output):-moz-ui-invalid {
    box-shadow: 0 0 1.5px 1px red;
}

To replicate in other browsers, I use:

input:invalid {
    box-shadow: 0 0 1.5px 1px red;
}

I played around with the pixel settings but I never would have guessed the 1.5px without looking at moz source.

To disable it, you can use:

input:invalid {
    box-shadow: none;
}
霞映澄塘 2024-11-12 23:54:03

尝试:

document.getElementById('myform').reset();

Try:

document.getElementById('myform').reset();
坏尐絯 2024-11-12 23:54:03

这是一个对我有用的非常简单的解决方案。我基本上将丑陋的红色更改为非常漂亮的蓝色,这是非必填字段的标准颜色,也是网络约定:

:required {
    border-color: rgba(82, 168, 236, 0.8);
}

Here is a very easy solution that worked for me. I basically changed the ugly red into a very nice blue, which is the standard color for non-required fields, and a web convention:

:required {
    border-color: rgba(82, 168, 236, 0.8);
}
哭泣的笑容 2024-11-12 23:54:03

这对我来说效果很好:

input:invalid {
     -moz-box-shadow: none;
}

This worked well for me:

input:invalid {
     -moz-box-shadow: none;
}
末骤雨初歇 2024-11-12 23:54:03

请尝试这个,

$("form").attr("novalidate",true);

对于您的全局 .js 文件或标头部分中的表单。

Please try this,

$("form").attr("novalidate",true);

for your form in your global .js file or in header section.

鹊巢 2024-11-12 23:54:03

我发现至少大部分解决这个问题的方法是:

<input type="text" name="example" value="This is an example" onInput="this.required = true;" />

这样,输入字段以漂亮的蓝色轮廓开始,但是一旦用户输入一个字符,它就会设置为必填(所以如果您输入一个字符然后退格,红色边界就在那里)。在这种情况下,用户可能会跳过输入,因此如果执行此操作,请确保进行后端验证。

A way I found to at least mostly fix the issue is:

<input type="text" name="example" value="This is an example" onInput="this.required = true;" />

That way, the input field starts out with that nice blue outline, but once the user enters a character it's set to required (so if you enter a character then backspace, the red border is there). In this case it is possible for a user to skip the input, so make sure to put backend validation in place if you do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文