使用数据属性进行表单validation而不是获取所有数据
我有一个表单,我试图validation。 我一直在使用id
和class
来获取元素,但我希望能够在一次传递中获得所有内容。 为一个部分而不是另一个部分使用class属性的原因是因为我允许用户添加其他地址信息。 我已经发布了以下所有代码。
这是validation脚本
var sections = ["general-information", "address-records", "employment-history", "driving-experience", "military-experience", "self-identification", "psp-notice", "eva-section"]; //array that houses the id tags of the application sections $('#application-form').submit(function(e){ e.preventDefault(); //stop the form from the default submission var application_info = new Object(); //start the application form Object //run through each section and gather info for(var i = 0; i < sections.length; i++){ application_info[sections[i]] = new Object(); //create a new object for each section //traverse each select by form control $("#"+sections[i]).find(".form-control").map(function (index, element){ $(element).each(function (index){ //application_info application_info[sections[i]][$(element).data('name')] = $('.'+$(element).data('name')).eq(index).val(); }); }).get(); } $.ajax({ url: '../assets/server/application_process.php', type : 'post', data : application_info, dataType : 'JSON', success : function (data){ } }); });
我希望能够保持相同的function,并允许脚本构建一个大对象。 如果需要更多信息,请告诉我。 我会尽可能多地提供。
而不是单击表单地址按钮时添加的其他表单元素中的额外数据
解释更多。
- 添加了哪些数据(如何在服务器上形成数据)?
- 这些数据是如何获取的?
- 如何将数据附加到表单中?
- 在附加额外数据后(在Web调试器中),表单看起来是什么样的?
- 似乎
success : function (data){ }
你什么都不做……
更新
- 好像你需要在谷歌查询:’jquery reinit dom’
- 以下是与你一样的问题: http : //forum.jquery.com/topic/reinitialize-document-ready-after-ajax-and-jquery
- 和jquery方法的链接: http : //api.jquery.com/live/将为所有与当前选择器匹配的元素附加一个事件处理程序, 现在和将来
-
live()
方法已被.on()
,因此使用.on()
来附加事件处理程序 : http :.on()
-
码:
$( ‘#应用程序的forms’)。提交(函数(E){…
试着转向:
$(’#application-form’)。on(’submit’,function(e){…