javascript - 对象上的 $.each() 函数 jQuery

标签 javascript jquery

我有一个表单,我正在尝试通过迭代元素并执行任何需要完成的操作来为其构建自定义验证。

这是我到目前为止的 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.

JsFiddle

  $.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/

相关文章:

jquery - 获取 span id 并将其作为 href 附加到先前的 span

javascript - 如何在 django 的 FormData 中附加更多数据?

javascript - 如何在点击选项卡内容时获取文本框

javascript - 循环根据长度精确迭代,但每次迭代插入相同的记录

javascript - 如何覆盖对象的默认输出?

javascript - 如何在 Collapse Jquery 中维护状态

javascript - 将动态创建的元素中的文本翻译为不同的格式

javascript - 如何从字符串显示html页面

javascript - 拨动开关在 Angular react 形式内不起作用

php - 使用 jqgrid PHP 相对于 jqgrid JS 的安全优势?