0

我有 AJAX 请求返回的以下标记:

<form id="quotation-form" name="quotation-form" action="" method="POST">
    <label for="site-name">Site Name:</label>
    <input type="text" id="site-name" name="site-name" />

    <label for="postcode">Site Postcode:</label>
    <input type="text" id="postcode" name="postcode" />

    <label for="bandwidth-requirement">Bandwidth Requirement:</label>
    <select id="bandwidth-requirement" id="bandwidth-requirement">
       <option value="10" selected>10Mb</option>
       <option value="20">20Mb</option>
       <option value="30">30Mb</option>
    </select>

    <input type="submit" name="quotation-form-submit" id="quotation-form-submit" value="Get Quotation" />

</form>

jQuery

$(document).on("submit", "#quotation-form", function() {
    // cache form input id's
    var site_name             = $('#site-name');
    var postcode              = $('#postcode');
    var bandwidth_requirement = $('#bandwidth-requirement');

    var valid = 1;

    // validate form input values
    if (site_name.val() == '') {
        name.addClass('error');
        valid = 0;
     } else {
         site_name.removeClass('error');
     }

     if (postcode.val() == '' || !checkPostcode(postcode.val())) {
         postcode.addClass('error');
         valid = 0;
     } else {
         postcode.removeClass('error');
     }

     if (bandwidth_requirement.val() == '') {
         bandwidth_requirement.addClass('error');
         valid = 0;
     } else {
         bandwidth_requirement.removeClass('error');
     }

});

错误是:

Uncaught TypeError: Object has no method 'addClass'

我在添加此验证之前尝试了一个警报,它工作正常,所以不确定问题是什么。

4

1 回答 1

1

您似乎没有name在此范围内的任何地方声明 var 。

您是否在此范围之外声明了它?我相信你可能想要:

if (site_name.val() == '') {
    site_name.addClass('error');
    valid = 0;
} else {
     site_name.removeClass('error');
}

您是否使用调试器单步执行?它可能会表明它name是空的......

于 2013-05-28T13:55:47.527 回答