Jquery脚本未在运行时加载编辑:意外令牌

时间:2015-03-24 03:42:09

标签: javascript jquery

为什么在运行时或激活提交按钮后没有加载此脚本?我从一个更大的脚本中取出它,它似乎应该运行adhoc,但是在页面加载后变量不会出现在控制台中,并且表单不会提交。我已经确认jquery已经正确加载了。任何人都有任何想法吗?

编辑:将脚本移动到页面底部,现在它运行但我收到了

Uncaught SyntaxError: Unexpected token <

不确定我是应该创建一个新主题还是保持开放状态,那么首选协议是什么?

    $('form.sidebar-form').submit(function(e) {

        e.preventDefault();

        var $form = $(this);
        var email = $.trim( $form.find('input[name="emailAddress"]').val() );
        var $response = $form.find('.form-response');

        // Loading Animation
        var iter = 1;
        var interval = 500;
        var timeout, loading;
        loading = function() {
            var base = '&bull;&nbsp;';
            var text = '&nbsp;';
            for(var i = 1; i <= iter; i++) {
                text = text + base;
            }
            $response.html(text);
            if( iter === 3 ) {
                iter = 0;
            } else {
                iter++;
            }
            timeout = setTimeout(loading, interval);
        };
        timeout = setTimeout(loading, interval);

        // Check Email Address
        var emailReg = /^([\w-\.\d\+]+@([\w-\d]+\.)+[\w-]{2,6})?$/;
        if( !emailReg.test( email ) || email == '' ) {
            clearTimeout( timeout );
            $response.html('<span class="error">Please enter a valid email address</span>');
            return;
        }

        // Eloqua Form Action
        var elqAction = 'https://s1224369867.t.eloqua.com/e/f2';

        // Post Data
        var postData = {
            elqFormName       : 'var-gameserver-0315',
            elqSiteId         : 'hidden',
            elqCampaignId     : '',
            emailAddress      : email,
            preferredLanquage : 'English',
            singleCheckbox10  : 'on',
            hiddenField       : 'var-Gameserver'
        };

        $.ajax({
            cache: false,
            data: postData,
            dataType: 'jsonp',
            headers: { 'cache-control': 'no-cache' },
            type: 'POST',
            url: elqAction
        }).always(function(data, status) {
            clearTimeout( timeout );
            if( status === 'success' || status === 'parsererror' ) {
                $response.html('<span class="success">Thanks for your interest in var</span>');

            } else {
                $response.html('<span class="error">There was a problem communicating with our server</span>');
            }
        });

    });

// HTML

                    <div class="sidebar">

                    <p>Please fill out the from below.</p>

                    <form class="sidebar-form">
                        <label for="emailAddress">Email Address<span style="">*</span></label>
                    <input id="field0" name="emailAddress" type="text" value="<eloqua type='emailfield' syntax='EmailAddress' />" />
                        <label for="firstName">First Name</label>
                    <input id="field1" name="firstName" type="text" value="<eloqua type='emailfield' syntax='FirstName' />" />
                        <label for="lastName">Last Name</label>
                    <input id="field2" name="lastName" type="text" value="<eloqua type='emailfield' syntax='LastName' />" />
                    <label for="title">Title</label>
                    <input id="field3" name="title" type="text" value="<eloqua type='emailfield' syntax='Title1' />" />
                        <label for="company">Company<span style="">*</span></label>
                    <input id="field4" name="company" type="text" value="<eloqua type='emailfield' syntax='Company' />" />
                        <label for="Phone">Phone<span style="">*</span></label>
                    <input id="field5" name="Phone" type="text" value="<eloqua type='emailfield' syntax='Business_Phone1' />" />
                        <input type="submit" value="Submit" class="form-submit" style="font-size: 100%; height: 24px; width: 100px" />
                    </form>
                    <div class="form-response"></div>
                    </div>

1 个答案:

答案 0 :(得分:1)

您是否尝试过将代码放入

$( document ).ready(function() {
    ... your code here ...
});

看起来像.form-response类的div不在表单标记中。

这行代码

var $response = $form.find('.form-response');

应该是

var $response = $('.form-response');

这是JSBin