代码之家  ›  专栏  ›  技术社区  ›  Veronica Amenamen

在addeventlistener wordpress中发布联系人表格7

  •  0
  • Veronica Amenamen  · 技术社区  · 6 年前

    我是javascript新手,我正在尝试使用 addeventlisterner 它在我的控制台中工作,但一旦我添加了ajax,它就不会。。。有人能检查一下我的代码吗?谢谢

    这是我的工作控制台代码

        document.addEventListener( 'wpcf7mailsent', function( event ) {
    
        var inputs = event.detail.inputs;
    
        if ( '1286' == event.detail.contactFormId ) {
    
            var fullname = document.getElementById('very-special-field').value; 
            var msisdn = document.getElementById('mobile-number').value; 
            var username = document.getElementById('username').value;
            var email = document.getElementById('email').value;
            var company = document.getElementById('company').value;
            var account_type_id = document.getElementById('account_type_id').value;
    
            form_data = {
                fullname,
                msisdn,
                username,
                email,
                company,
                account_type_id
    
            }
    
            alert('hi');
    
            console.log(form_data);
    
        }
    
    `}, false );
    

    但是如果我添加ajax,它就不起作用了

    document.addEventListener( 'wpcf7mailsent', function( event ) {
    
        var inputs = event.detail.inputs;
    
        if ( '1286' == event.detail.contactFormId ) {
    
            var fullname = document.getElementById('very-special-field').value; 
            var msisdn = document.getElementById('mobile-number').value; 
            var username = document.getElementById('username').value;
            var email = document.getElementById('email').value;
            var company = document.getElementById('company').value;
            var account_type_id = document.getElementById('account_type_id').value;
    
            form_data = {
                fullname,
                msisdn,
                username,
                email,
                company,
                account_type_id
    
            }
            $.ajax({
              type: "POST",
              url: http://myurl.com,
              data: JSON.stringify(form_data),
              success: success,
             dataType: 'json'
            });
    
            alert('hi');
    
            console.log(form_data);
    
        }
    
    }, false );
    

    有人能帮我吗?:(我这样做对吗?

    1 回复  |  直到 5 年前
        1
  •  0
  •   Vel    6 年前

    尝试此脚本

        document.addEventListener( 'wpcf7mailsent', function( event ) {
    
            var inputs = event.detail.inputs;
    
            if ( '1286' == event.detail.contactFormId ) {
    
                var fullname = document.getElementById('very-special-field').value; 
                var msisdn = document.getElementById('mobile-number').value; 
                var username = document.getElementById('username').value;
                var email = document.getElementById('email').value;
                var company = document.getElementById('company').value;
                var account_type_id = document.getElementById('account_type_id').value;
    
                form_data = {
                    fullname,
                    msisdn,
                    username,
                    email,
                    company,
                    account_type_id
    
                };
    
                jQuery.ajax({
                  type: "POST",
                  url: 'http://myurl.com',
                  data: JSON.stringify(form_data),
                  success: function(data) {
                    console.log(data);
                  },
                  error: function(data) {
                    console.log(data);
                  },
                 dataType: 'json'
                });
                alert('hi');
                console.log(form_data);
            }
    
        }, false );