2011-11-26 55 views
-2

我創建了可以工作的代碼,它發佈了一個ajax調用。它還發布成功消息。我想添加驗證點擊功能。所以如果驗證是真的,它只會運行$ .ajax。我有一個表單驗證腳本。如果默認爲true,jQuery 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; 
     }); 
    }) 

我想運行在$ 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> 
+0

什麼是「語法問題」?什麼是不起作用或導致錯誤的代碼(以及錯誤究竟是什麼)? – Pointy

+0

'eval(「document.subscribeform.elements ['」+ fieldstocheck [i] +「']。type」=== document.subscribeform.elements [fieldstocheck [i]] .type'。 – lonesomeday

回答

2

亞歷克斯,

的checkForm函數返回一個布爾值,指示成功或失敗,然後把if語句如下只是阿賈克斯以上,但click事件裏面,所以你只有在checkform爲true時調用ajax,否則編寫else部分以提醒用戶修正錯誤。

​​

在一個側面說明,我提醒你極高不要只依賴(客戶端)驗證的JavaScript - 你需要確保你通過AJAX稱爲服務器腳本還會進行檢查以確保形式發佈數據並沒有被篡改來破解你。諒解。

[R

+0

我試過這個解決方案,但得到了這個錯誤從螢火蟲語法錯誤:遺失;之前的語句 –

+0

啊我沒有把它放在函數中,但在click()。謝謝 –

0

對於功能checkgroup和其他一些功能,你問的函數返回一個true,則已經有了後return false;。當它進入return false;時,該功能停在那裏。所以你需要做的是決定你是否想要一個return false或返回true。不要把return true;沒有其他人

+0

我想點擊返回false我只是想要jquery .show函數內運行揭露分區感謝羅斯幫助我解決這個問題。 –