gpt4 book ai didi

javascript - $(elem) 的目的是什么?

转载 作者:行者123 更新时间:2023-11-30 08:08:36 27 4
gpt4 key购买 nike

$(elem) 的用途是什么?我什么时候使用它?

我的 javascript 函数有问题,我有 3 个表单,这 3 个表单有时共享相同的字段类,因此当我尝试对其中一个表单进行验证时,即使它已正确填写,它也会返回一个错误,因为该函数将获得另一种形式的类,我的一个 friend 说使用 $(elem) 因为它可以解决他的问题,但我不知道如何使用 $(elem),何时使用它,以及它是如何工作的。

如果有人能向我解释一下,那就太好了。

一个例子也将不胜感激:)

例子:

function dispFields(a, b, c, z, ch) 
{
var valid;
var blanks = Array();
var blanks2 = Array();
var email_reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
var myVal = $('#'+a).find('input.required').map(function() {
return $(this).val();
}).get().join(',');
var myTitle = $('#'+a).find('input.required').map(function() {
return $(this).attr("title");
}).get().join(',');

var myEmail = $('#'+a).find('input.email-field').map(function() {
return $(this).val();
}).get().join(',');

var myEmailtitle = $('#'+a).find('input.email-field').map(function() {
return $(this).attr("title");
}).get().join(',');

var email_val = myEmail.split(',');
var email_title = myEmailtitle.split(',');
var email_error = email_val.length;
var error_form = myVal.split(',');
var error_title = myTitle.split(',');
var errorlength = error_form.length;

String.prototype.email_match = function(s){
return this.match(s)!==null
}
for(y=0;y<email_error;y++){
if (!('indexOf' in Array.prototype)) {
Array.prototype.indexOf= function(finds, is /*opt*/) {
if (is===undefined) is= 0;
if (is<0) is+= this.length;
if (is<0) is= 0;
for (var n= this.length; is<n; is++)
if (is in this && this[is]===finds)
return is;
return -1;
};
}
if((error_title.indexOf(email_title[y])) == -1){
if(email_val[y] == ''){
valid = true;
}else{
if(email_reg.test(String(email_val[y]).toLowerCase())==false){
blanks2[y] = email_title[y]+" - Should be valid email";
valid = false;
}
}
}
}
for(x=0;x<errorlength;x++){
if(error_form[x] == ''){
if(myTitle == ''){
valid = true;
}else{
blanks[x] = "Required - "+error_title[x];
valid = false;
}
}else{
var myBool = (String(error_title[x]).toLowerCase()).email_match("email");
if(myBool == true){
if(email_reg.test(String(error_form[x]).toLowerCase())==false){
blanks[x] = error_title[x]+" - Should be valid email";
valid = false;
}
}else{
if(blanks == ''){
valid = true;
}
}
}
}
if(blanks == '' && blanks2 == ''){
valid = true;
}else{
valid = false;
}

var blankc = blanks.concat(blanks2);
var requiredS = blankc.toString();
var nblank = requiredS.split(',').join("\n");

if(valid != true){
alert("Please review:\n\n"+nblank);
document.getElementById(a).style.display = "";
document.getElementById(b).style.display = "none";
}else{
var ssplits = z.split(',');
var fieldlengths = ssplits.length;
var indexes = fieldlengths - 1;
var d;
var e;
d=parseInt(c,10)+1;
e=d-1;

if(b == ssplits[indexes]){
$("#step"+d).addClass("active");
$("#step"+e).removeClass("active");
document.getElementById(a).style.display = "none";
document.getElementById(b).style.display = "";
document.getElementById("tbutton").style.display = "";
document.getElementById("tcont").style.display = "none";
document.getElementById("captchas").style.display = "none";
}

$("#step"+d).addClass("active");
$("#step"+e).removeClass("active");
document.getElementById(a).style.display = "none";
document.getElementById(b).style.display = "";
if(b == ch){
document.getElementById("captchas").style.display = "";
}
}
return false;
}

现在每当我提交表单时都会调用此函数,但在一个页面中我可以有 2 或 3 个表单可能具有类“必需”因此即使表单中的字段已填满它仍然会提示错误因为它正在获取其他表单字段的类。

最佳答案

$(通常)是 jQuery功能。

如果您使用的是 jQuery,则 $(elem) 会将 elem 包装为一个 jQuery 对象,并让您可以访问其所有实用方法。

您的 friend 可能建议您使用 jQuery,这样您就可以执行以下操作:

if ($(elem).hasClass('<your validation class>')) {

编辑 - 查看您刚刚添加的代码,您似乎已经在使用 jQuery。如果没有您 friend 所说内容的更多上下文,很难推测其含义。

如果您有关于如何使用 jQuery 完成某事的具体问题,您可以随时发布一个包含更精简代码片段的新问题。

关于javascript - $(elem) 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13866433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com