html - 输入 [type ="file"] 使用 css 检查文件附件是否存在

标签 html css input

这不是一个 JS 问题,如果可能的话,只是寻找一个清晰的 css 解决方案。

对于单选按钮或复选框,我们可以使用 :checked 伪类:

input{
  % styles %
}
input:checked{
  % another styles %
}

是否有使用 CSS 检查文件附件是否存在的技巧?

最佳答案

(已更新,由于误解了问题)

可以使用 required 属性检查文件是否已附加。

参见:http://jsfiddle.net/1ua59jt1/1/

HTML:

<input id="file" type="file" name="fileselect" required="required" />

CSS:

#file:valid { color: green; }
#file:invalid { color: red; }

但是如果以这种方式选择了“正确”的文件,您永远无法真正验证。您只能检查文件是否被选中。


//完成更新:

required 属性是 HTML5。请注意,并非所有浏览器都支持此功能:http://www.w3schools.com/tags/att_input_required.asp

这意味着客户端验证唯一值得信赖的方式是使用 javascript。如果您想这样做,我建议您在表单上使用 novalidate 属性来禁用浏览器的默认验证 ( http://www.w3schools.com/tags/att_form_novalidate.asp)。

也始终使用服务器端验证。您永远无法确保用户启用了 jaavscript 或没有操作 javascript。因此,最好的方法始终是在您完全控制的一侧验证值:服务器,即表单将其数据发送到的操作。

关于html - 输入 [type ="file"] 使用 css 检查文件附件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32041782/

相关文章:

html - 如何在 Bootstrap 中禁用 <pre> 上的样式

jquery - 更改数据角色 ="header"中 .ui-title 的宽度

html - Chrome 在使用 column-count 时为其容器添加了额外的空间

c - 使用打印功能无法打印字符串的问题

jquery - 如何在鼠标悬停时滚动图像?

html - 对齐 block 中的元素

javascript - 根据第一个单元格的值在 JavaScript 中隐藏表行

javascript - getJSON更新进度条

html - Fieldset 中的 DIV 层在后台打开而不是前景

reactjs - shadcn 输入+表格+Zod "A component is changing an uncontrolled input to be controlled"