简体   繁体   English

HTML对象的流星模板数组

[英]meteor template array of html objects

I'm trying to work out how best to handle an input form that is for order processing that basically has a number of identical lines on it for ordering items of clothing. 我正在尝试找出如何最好地处理用于订单处理的输入表单,该表单上基本上具有用于订购服装项目的许多相同行。

The controls are all drop down selectors (4 different ones to a line). 控件都是下拉选择器(一行中有4个不同的选择器)。

At the moment I've just give each one a unique ID and am working through them one by one, but that strikes me as very inefficient. 目前,我只是给每个人一个唯一的ID,然后一个一个地处理它们,但这令我感到效率很低。

Is there some way I can use a loop in the html (I don't think this can be done) or some other way to use an array of controls and just iterate over them on the form submit? 有什么方法可以在html中使用循环(我认为不能做到这一点),或者有其他方法可以使用控件数组并仅在表单提交上对其进行迭代?

Meteor projects usually include jQuery. 流星项目通常包括jQuery。 jQuery is pretty useful for grabbing groups of elements and running them through a function so that you don't have to repeat yourself as much. jQuery对于获取元素组并通过函数运行它们非常有用,这样您就不必重复太多。

In addition to unique ids, you can add the same class to all or a group of the form elements, and then in the onSubmit callback grab them all with a class selector in jQuery and send them all to a function that combines them into a useful object, eg 除了唯一的ID外,您还可以将相同的类添加到所有或一组表单元素中,然后在onSubmit回调中使用jQuery中的类选择器将它们全部获取,并将它们全部发送到将它们组合为有用的函数中对象,例如

// collect the form inputs in class salesform into an object

var formResult = {};
$(".salesform").each(function(el){ formResult[$(this)[0].id]=$(this).val() });

// form ids and values are now in formResult

There may also be other selectors you can use instead of tagging the elements you want with a class. 您可能还可以使用其他选择器,而不是使用类标记所需的元素。

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

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