您的位置:

如何使用form-horizontal提高网页表单的可访问性

表单是Web应用程序的核心功能,它们用于收集各种数据并与用户交互。 随着可访问性对Web开发的要求越来越高,使Web表单具有更好的可访问性对于人们来说变得越来越重要。因此,在此文中,我们将详细介绍如何使用form-horizontal提高网页表单的可访问性。

一、什么是form-horizontal表单

在介绍如何使用form-horizontal提高网页表单的可访问性之前,先来了解一下什么是form-horizontal表单。在Bootstrap框架中,form-horizontal表单是一种特殊的表单,它将行元素水平对齐而不是垂直对齐。通过这种方式,可以更好地利用水平空间,并使表单更易于阅读和填写。

二、为什么需要提高网页表单的可访问性

Web表单对于所有用户来说都是至关重要的,尤其是对于那些有视觉障碍或其他障碍的人来说。良好的可访问性可以帮助这些用户更好地理解和使用表单。通过提高表单的可访问性,您可以帮助您的用户更好地与您的应用程序交互,增加应用程序的易用性和可靠性。

三、如何使用form-horizontal提高网页表单的可访问性

1. 使用label元素

在form-horizontal表单中,使用label元素为输入字段提供标签。 这使得用户能够更轻松地识别和填写每个字段。 同时,label元素可以与相关的输入字段绑定,以便屏幕阅读器可以识别它们并帮助有视觉障碍的用户进行填写。


<div class="form-group">
  <label for="inputUsername" class="col-sm-2 control-label">用户名</label>
  <div class="col-sm-10">
    <input type="text" class="form-control" id="inputUsername" placeholder="请输入用户名">
  </div>
</div>

2. 使用fieldset和legend元素

使用fieldset和legend元素可将表单组织成逻辑部分, 使其更易于阅读和理解。HTML5中表单元素中增加了较多新特性,包括对是否必填的标注,但是对于那些不理解英文或是不熟悉表单的人来说更好的做法就是加入一些辅助性文字说明,并且可以使用legend元素来对fieldset进行更明确的解释,以帮助视力受损用户填写表单。


<fieldset>
  <legend>个人信息</legend>
  <div class="form-group">
    <label for="inputName" class="col-sm-2 control-label">姓名</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="inputName" placeholder="请输入姓名">
    </div>
  </div>
  ...
</fieldset>

3. 使用aria-label属性

在没有label元素的情况下,可以使用aria-label属性为输入字段提供标签。这是一种属性,可使屏幕阅读器将文本读取为输入字段标签,并且有助于提高表单的可访问性。


<div class="form-group">
  <div class="col-sm-10">
    <input type="checkbox" class="form-control" id="inputCheckbox" aria-label="请勾选该复选框">
    <label for="inputCheckbox" class="control-label">复选框</label>
  </div>
</div>

4. 使用tabindex属性

tabindex属性用于指定在用户按Tab时输入字段的访问顺序。为了提高表单的可访问性,应该按照逻辑顺序定义输入字段的tabindex属性,以便用户可以轻松地通过Tab键在表单中导航。


<form>
  <div class="form-group">
    <label for="inputName" class="col-sm-2 control-label">姓名</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="inputName" placeholder="请输入姓名" tabindex="1">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword" class="col-sm-2 control-label">密码</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword" placeholder="请输入密码" tabindex="2">
    </div>
  </div>
  ...
</form>

5. 设置适当的input类型

对于不同类型的数据,应该始终使用适当的HTML input类型。以email类型为例,用email类型取代text类型,可以让浏览器自动验证输入是否正确。这不仅减少了用户错误,而且可以提高表单的可访问性。


<div class="form-group">
  <label for="inputEmail" class="col-sm-2 control-label">邮箱</label>
  <div class="col-sm-10">
    <input type="email" class="form-control" id="inputEmail" placeholder="请输入邮箱">
  </div>
</div>

四、结论

通过使用form-horizontal表单以及上述许多技术来增加表单的可访问性,用户可以更轻松地输入和提交数据,并享受更高效的用户体验。 良好的可访问性可以使web应用程序更易于使用和更具可靠性,因此为用户提供高质量的表单体验很重要。

如何使用form-horizontal提高网页表单的可访问性

2023-05-18
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)