jQuery を標準ライブラリとして使用しています。ただし、単純な機能を実行するには Prototype を使用する必要がありました。しかし、これは衝突を引き起こしました!
以下のコードを jQuery で動作させ、プロトタイプを配布するにはどうすればよいですか?
<script type="text/javascript" language="javascript">
function trim(str){str = str.replace(/^\s*$/, '');return str;}
function signup() {
var email = trim($F("email"));
var name = trim($F("name"));
//EMAIL VALIDATION
var goodEmail = email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\.arpa)|(\.asia)|(\.cat)|(\.int)|(\.jobs)|(\.tel)|(\.travel)|(\.xxx)|(\..{2,2}))$)\b/gi);
apos=email.indexOf("@");dotpos = email.lastIndexOf(".");lastpos=email.length-1;
var badEmail = (apos<1 || dotpos-apos<2 || lastpos-dotpos<2);
if (name=="") {
$("myResponse").style.display="inline"; //3 lines to show and style the error message
$("myResponse").style.color="white"; // there are more ways to do it using css classes for example.
$("myResponse").innerHTML="Por favor, digite seu nome"; // you could also make an error function.
$("name").clear(); $("name").focus(); // clear the field and put cursor inside
return false;
/* YOU CAN REPEAT THE ABOVE ELSE IF BLOCK IF YOU HAD OTHER FIELDS IN YOUR FORM,
LIKE LAST NAME, ADDRESS ETC. AS AN EXAMPLE SEE HOW THE NAME IS HANDLED AND REPEAT
*/
}
else if (email=="" || !goodEmail || badEmail) {
$("myResponse").style.display="inline"; //3 lines to show and style the error message
$("myResponse").style.color="white";
$("myResponse").innerHTML="Por favor, insira um email válido";
$("email").focus();
return false;
}
else {
//YOU MAY WANT TO CHANGE THE URL IN THE LINE BELOW
var url = "includes/optIn.php";
var params = $("subform").serialize();
new Ajax.Request(url, {onComplete:showResponse, onException:showException, onFailure:showException, asynchronous:true, method:'post', evalScripts:false, postBody:params});
$("submit", "myResponse").invoke('hide'); // Hide the buttom and the message
$("loading").show(); // show the loading image.
return false;
}
}
function showResponse(req) {
$("loading").hide();
$("myResponse").innerHTML=req.responseText; //Writes the "Thank you" message that comes from optIn.php and styles it.
$("myResponse").style.display="inline";
$("myResponse").style.color="white";
$("submit").show();
$("name", "email").invoke('clear');
}
function showException(req) {
$("myResponse").innerHTML=req.responseText;
alert("A comunicação com o servidor falhou. Tente novamente!");
$("loading", "myResponse").invoke('hide');
$("submit").show();
$("name", "email").invoke('clear');
}
</script>