我創建了可以工作的代碼,它發佈了一個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>
什麼是「語法問題」?什麼是不起作用或導致錯誤的代碼(以及錯誤究竟是什麼)? – Pointy
'eval(「document.subscribeform.elements ['」+ fieldstocheck [i] +「']。type」=== document.subscribeform.elements [fieldstocheck [i]] .type'。 – lonesomeday