表单提交导致'超出最大调用堆栈大小'

时间:2015-07-13 09:10:36

标签: javascript jquery html forms

我已经创建了一个用于审核网站上某个人的表单,但是在提交表单时没有任何操作,并且控制台显示超出最大调用堆栈#39;希望有人可以帮助指出我的代码中的错误,阻止它工作。这是表单代码:

 <form id="fReviewMe" method="post" action="/process/review-p.cfm" enctype="multipart/form-data" style="display:none">
    <label style="padding-top:10px"><i class="fa fa-asterisk magenta"></i> Your Name:</label>
    <input type="text" name="uname" id="uname" class="span8" placeholder="Please tell us your name">
    <label><i class="fa fa-asterisk magenta"></i> Your Business Name:</label>
    <input type="text" name="business" id="business" class="span8" placeholder="Please tell us your business name">
    <label><i class="fa fa-asterisk magenta"></i> Your Review:</label>
    <textarea name="reviewmsg" id="reviewmsg" class="span8" rows="8" placeholder="Please add your review in here"></textarea>
    <!--- form errors --->
            <div id="dFormErrors" class="row" style="display:none;padding-bottom:20px;">
              <span id="sEMessage" class="pull-right"></span>
              <i class="fa fa-asterisk magenta pull-right"></i>
            </div>

   <!--- form buttons --->
  <div id="dButtonsReviewForm" class="row">
      <a onClick="checkForm()" name="submit" class="cta pull-right">Submit</a>
      <a href="javascript:hideReviewMe()" class="pull-right cta" style="margin-right:6px">Cancel</a>
   </div>
  <!--- form saving --->
      <div id="dSavingReviewForm" style="display:none">
          <span class="pull-right hibuBtn" style="cursor:wait"><i class="fa fa-spinner fa-spin"></i> Saving</span>
      </div>

这是JS代码:

/* intercept submit event */
$( "#fReviewMe" ).submit(function(event) {
   checkForm();
   event.preventDefault();
});

/* form validation */
function checkForm(){

  var errors = 0;
  var cuname = $('#uname').val();
  var ccompany = $('#business').val()
  var creview = $('#reviewmsg').val()
  var cstars = $('#rStar').val()
  var eMessage = "";
  $('#dFormErrors').hide();
  $('input').removeClass('validFalse');
  $('textarea').removeClass('validFalse');

if ($('#tnc').is(':checked')) {
 }
 else{
    eMessage = "Please tick to accepts out terms and conditions";
    errors++;   
}

if(cstars.length < 1){
  $("input").blur();
  $("textarea").blur();
  eMessage = "Please choose a star rating";
  errors++;
}

if(creview.length < 1){
  $('#reviewmsg').focus();
  $('#reviewmsg').addClass('validFalse');
  eMessage = "Please add your review";
  errors++;
}
else{
  $('#reviewmsg').addClass('validTrue');
}

if(ccompany.length < 1){
  $('#business').focus();
  $('#business').addClass('validFalse');
  eMessage = "Please tell us your business name";
  errors++;
}
else{
  $('#business').addClass('validTrue');
}

if(cuname.length < 1){
  $('#uname').focus();
  $('#uname').addClass('validFalse');
  eMessage = "Please tell us your name";
  errors++;
}
else{
   $('#uname').addClass('validTrue');
}

/* check errors and submit */
if(errors > 0){
  $('#dFormErrors').slideDown();
  $('#sEMessage').html(eMessage)
}
else{
  $('#dButtonsReviewForm').hide();
  $('#dSavingReviewForm').show();
  $('#fReviewMe').submit();
}

};

提前致谢

1 个答案:

答案 0 :(得分:33)

$('#fReviewMe').submit();替换为:

$('#fReviewMe')[0].submit();

调用DOM节点方法submit以避免提交jQuery处理程序&#39;循环&#39;。