简体   繁体   English

动态添加表单元素的可访问方式(jQuery)

[英]Accessible way to add form elements dynamically (jQuery)

I'm setting up a form in which multiple entries can potentially be made (via an Add more button) for one of the qestions. 我正在设置一种表单,其中可以潜在地(通过“添加更多”按钮)为多个问题之一输入多个条目。 I'm trying to figure out the best way to make this accessible without javascript and also to allow the input fields to be shown dynamically each time the Add button is clicked. 我试图找出最好的方法来使它无需使用JavaScript就可以访问,并且还允许每次单击“添加”按钮时动态显示输入字段。 There are 3 pieces of data i need to collect for each entry, below is the basics of what the HTML will look like: 我需要为每个条目收集3条数据,以下是HTML外观的基础知识:

Type: <input name = "type[]" id = "type" />
Sub type: <input name = "subtype[]" id = "type" />
Number: <input name = "num[]" id = "type" />

I don't have a problem with the JS code required to create this content dynamically on each click of the Add button, but I am thinking that as it is reasonable to set a limit on the amount of rows a person could add (for this type of data, 10 entries would be the very maximum i would expect), I could just add ten empty fields to the HTML form. 每次单击“添加”按钮时,动态创建此内容所需的JS代码都没有问题,但我认为,合理地设置一个人可以添加的行数是合理的(为此数据类型,最多可以输入10个条目),我可以在HTML表单中添加十个空字段。 Then, my jQuery code would hide them on domready, then each time the Add button is clicked, it simply shows the next available empty field on the form (until it gets to the 10th, at which point the Add button is disabled). 然后,我的jQuery代码会在domready时将其隐藏,然后每次单击“添加”按钮时,它只会在表单上显示下一个可用的空字段(直到第10位,此时“添加”按钮被禁用)。

Does this approach make any sense? 这种方法有意义吗?

Yes, you've described the usual progressive-enhancement way of handling dynamic rows. 是的,您已经描述了处理动态行的通常的渐进增强方法。

You can always compromise as well: put n spare rows on the page as you describe so that a non-JS user can only add n rows at a time, but use JS to clone/add new rows as necessary so that JS users can add as many as they like. 您也总是可以妥协:按照您的描述在页面上放置n条备用行,以便非JS用户一次只能添加n行,但根据需要使用JS克隆/添加新行,以便JS用户可以添加尽可能多。

(If the user can come back and add more rows afterwards, and it's not likely a non-JS user will want to add many rows at once, you can just make n =1 for the simple version: an empty row which is removed from the document by script and cloned back into the document as needed.) (如果用户可以回来然后再添加更多行,并且非JS用户不太可能希望一次添加很多行,则只需将n = 1作为简单版本即可:将空行从文档(按脚本),然后根据需要克隆回文档中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM