如何使这段代码与jQuery一起工作
How to make this code work with jQuery?
我使用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>
你试过jQuery.noConflict()吗?
http://api.jquery.com/jQuery.noConflict/对于jQuery,您需要更改选择器以使用#
id选择器。此外,jQuery不区分集合和单个元素,它总是调用集合中每个元素的方法。
特别是,您将使用.val()
, .css()
, .show()
, .hide()
和.focus()
方法来取代Prototype和本机DOM方法。此外,您将切换到jQuery.ajax
函数-在API文档中查找它们。
以下是您需要完成的一些替换
$F('id') => $('#id').val();
//directly
$("id").style.display='inline' => $('#id')[0].style.display='inline';
//the "jQuery" way
$("id").style.display='inline' => $('#id').css('display','inline')
$("id").style.color="white" => $("#myResponse").css('color',"white")
$("id").innerHTML="Por favor, digite seu nome" => $('#id').html("Por favor, digite seu nome")
//.clear() has no direct equivalent method but this is close
$("id").clear() => $('#id').val(null)
$("subform").serialize() => $("#subform").serialize()
$("submit", "myResponse").invoke('hide') => $("#submit, #myResponse").hide()
这些都是简单的替换,我把Ajax.Request()
转换成$.ajax()
留给你去做
相关文章:
- 将FormParams与Jersey和jQuery一起使用
- 存储数据的最佳方式是什么,以便与jquery一起使用
- 在使用 Django 静态文件时动态设置映像时,只有绝对路径才能与 jquery 一起使用
- 使一组对象与 jQuery 一起拖动
- 将(this)与动态内容jQuery一起使用
- 将页面Url与jQuery一起使用
- 如何找到元素'的onClick处理程序实际上是与jQuery一起添加的
- 400错误请求仅使用AngularJs,与jQuery一起使用
- 引导设置单选按钮与 jquery 一起检查
- 将 ajax 与 jQuery 一起使用 - URL 不被接受
- 如何将 fadeIn 与 css 属性更改 jquery 一起使用
- 我应该如何初始化这个变量以与jQuery一起使用
- 单击按钮更改按钮上的 ID 值,单击新 ID 更改回原始 ID 与 Jquery 一起使用
- 将其与伪选择器 jquery 一起使用
- 使类在由 jquery 更新后与 jquery 一起使用
- 如何引用要与 HTML 和 jQuery 一起使用的 XML 文件/数据
- 如何将foreach循环与JQuery一起使用
- 将 $_GET 与 Jquery 一起使用
- 如何将表与 jquery 一起使用
- 将.html与JQuery一起使用,不会将数组中的所有项发布到html