如果默认为true,则JQuery Ajax运行成功回调

时间:2011-11-26 19:40:52

标签: jquery ajax

我创建了有效的代码,它发布了一个ajax调用。它还会发布成功消息。我想为点击功能添加验证。因此,如果验证为真,它只运行$ .ajax。我有一个表单验证脚本。

这是一个语法问题我找不到什么

AJAX

  $(document).ready ( function () {
        $('#go').click (function () {


       $.ajax ( {
                type:       'POST',
                data:       $('#newsletter').serialize (),
                url:        $('#newsletter').attr ('action'),
                success:   function(){



                                $('#thankYou').show (
                                    1000,
                                    function () {
                                        setTimeout ( function() { $('#thankYou').hide(1000); }, 3000);
                                    }
                                );


            }

            } );
            return false;
        } );
    } )

我想在$('#go')中运行checkform()。点击并且仅当表单传递时运行$ .ajax作为checkform的回调。

function checkform() {
  for (i=0;i<fieldstocheck.length;i++) {
    if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].type") == "checkbox") {
      if (document.subscribeform.elements[fieldstocheck[i]].checked) {
      } else {
        alert("Please enter your "+fieldnames[i]);
        eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
        return false;
      }
    }
    else {
      if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value") == "") {
        alert("Please enter your "+fieldnames[i]);
        eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
        return false;
      }
    }
  }
  for (i=0;i<groupstocheck.length;i++) {
    if (!checkGroup(groupstocheck[i],groupnames[i])) {
      return false;
    }
  }

  if(! compareEmail())
  {
    alert("Email Addresses you entered do not match");
    return false;
  }
  return true;
}

var fieldstocheck = new Array();
var fieldnames = new Array();
function addFieldToCheck(value,name) {
  fieldstocheck[fieldstocheck.length] = value;
  fieldnames[fieldnames.length] = name;
}
var groupstocheck = new Array();
var groupnames = new Array();
function addGroupToCheck(value,name) {
  groupstocheck[groupstocheck.length] = value;
  groupnames[groupnames.length] = name;
}

function compareEmail()
{
  return (document.subscribeform.elements["email"].value == document.subscribeform.elements["emailconfirm"].value);
}
function checkGroup(name,value) {
  option = -1;
  for (i=0;i<document.subscribeform.elements[name].length;i++) {
    if (document.subscribeform.elements[name][i].checked) {
      option = i;
    }
  }
  if (option == -1) {
    alert ("Please enter your "+value);
    return false;
  }
  return true;
}

标记

<div id="container">
<form id="newsletter"  method="post" action="http://www.officeyoganyc.com/lists/?p=subscribe" name="subscribeform"><input type="hidden" name="formtoken" value="a7d1884b463ed70e91fb62a5121e9846" />

<div id="fieldWrapper">
<div class="fieldHolder">
  <div class="attributeinput1"><input type=text name=email value="email" autofocus="autofocus" autocomplete="on" size="12"/> 
  <script language="Javascript" type="text/javascript">addFieldToCheck("email","Email");</script></div> 
  </div>


  <div class="fieldHolder2">
  <div class="attributeinput2"><input type=text name=emailconfirm value="confirm email" autocomplete="off" size="12"/> 
  <script language="Javascript" type="text/javascript">addFieldToCheck("emailconfirm","Confirm your email address");</script></div>
        </div> 
        </div>
  <input type="hidden" name="list[1]" value="signup">
  <input type="hidden" name="listname[1]" value="office yoga list"/>
  <div style="display:none"><input type="text" name="VerificationCodeX" value="" size="20"></div>

<input type="hidden" name="subscribe" value="Subscribe"/>
<div id="subscribe"><input type=image src="http://www.officeyoganyc.com/themes/zen/zen/images/yogaSubmit.png" id="go" name="subscribe" value="Subscribe onClick="return checkform();"></div>
    </form>

    </div>
  <div id="thankYou">Thank You For Signing Up</div>

2 个答案:

答案 0 :(得分:2)

亚历,

checkForm函数返回一个表示成功或失败的布尔值,然后将一个if语句放在ajax上面,但在click事件中,所以你只有在checkform给出的情况下才调用ajax,否则写下else部分来提醒用户修复任何错误。

if(checkform()) {
   $.ajax(.....)
} else {
   alert("Validation Failed");
}

另一方面,我提醒您极度不要仅仅依赖于javascript(客户端)验证 - 您需要确保ajax调用的服务器脚本还要检查以确保表单发布数据没有'被篡改为黑客攻击你。预先警告。

[R

答案 1 :(得分:0)

对于函数checkgroup和其他一些函数,您要求函数在已经有return false;后返回true。当它遇到return false;时,函数就会停在那里。所以你需要做的是决定你是想要return false还是返回true。不要在没有else

的情况下放置return true;