我有一个表单,我正在尝试通过迭代元素并执行任何需要完成的操作来为其构建自定义验证。
这是我到目前为止的 js 代码
$('#createClient').click(function (event) {
var formData = {
'clientbrandname': $("#createClientBrand").val(),
'surveyReferral': $("#createSurveyReferral").val(),
'GuestReferral': $("#createGuestReferral").val(),
'email': $("#createInviteEmail").val(),
'phone': $("#createPhoneNumber").val(),
'xampAccNumber': $('#createUserLimit').val(),
'logo': $('#createClientLogo').val(),
'Template': $('#createRefPage').val(),
'xampClient': $('#createAmplifierClient').val(),
'xampActive': $('#createAmpMembership').val(),
'sReferralID': $('#sReferralID').val(),
'gReferralID': $('#gReferralID').val()
};
$(formData).each(function () {
alert($(this).val());
});
});
使用这段代码,我遇到了一个错误(i.nodeName is undefined
),我怀疑该错误来 self 尝试使用 $(this).val( )
有人可以建议这个问题的解决方案吗?
最佳答案
您正在迭代一个对象。因此,您需要在函数中调用名称值对。
The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array.
$.each( formData, function( name, value ) {
alert(name); //Alerts clientbrandname
alert(value); //Alerts the value from "$("#createClientBrand").val()"
});
关于javascript - 对象上的 $.each() 函数 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21416443/