JavaScript函数不返回任何值
问题描述:
我想启用提交按钮,如果所有的字段填入正确的模式,但它不工作。我试图在每个函数的变量中存储返回值,但没有做出任何更改,所有验证都运行良好,但禁用的提交按钮未启用。JavaScript函数不返回任何值
我不能让一个变量返回的值。任何帮助都是有好处的。这里是我的代码:
$(document).ready(function(){
function Val_Fname()
{
$("#f_name").keyup(function(){
var fn = $(this).val();
if(fn.length<=0)
{
$(this).attr("Placeholder","Required Field");
$(this).attr("style","color:red;");
return 0;
}
else
{
var reg = /^[A-Za-z]+$/;
if(reg.test(fn))
{
$(this).attr("style","color:Black;");
return 1;
}
else
{
$(this).attr("style","color:red;");
return 0;
}
}
});
}
function Val_Mname()
{
$("#m_name").keyup(function(){
var mn = $(this).val();
if(mn.length<=0)
{
$(this).attr("Placeholder","Required Field");
$(this).attr("style","color:red;");
return 0;
}
else
{
var reg = /^[A-Za-z]+$/;
if(reg.test(mn))
{
$(this).attr("style","color:Black;");
return 1;
}
else
{
$(this).attr("style","color:red;");
return 0;
}
}
});
}
function Val_Lname()
{
$("#l_name").keyup(function(){
var ln = $(this).val();
if(ln.length<=0)
{
$(this).attr("Placeholder","Required Field");
$(this).attr("style","color:red;");
return 0;
}
else
{
var reg = /^[A-Za-z]+$/;
if(reg.test(ln))
{
$(this).attr("style","color:Black;");
return 1;
}
else
{
$(this).attr("style","color:red;");
return 0;
}
}
});
}
function Val_em()
{
$("#email").keyup(function(){
var em = $(this).val();
if(em.length<=0)
{
$(this).attr("placeholder","Email can not be blank");
$(this).attr("style","color:red;");
return 0;
}
else
{
var reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
if(reg.test(em))
{
$(this).attr("style","color:Black;");
return 1;
}
else
{
$(this).attr("style","color:red;");
return 0;
}
}
});
}
function Val_en()
{
$("#enroll").keyup(function(){
var enroll = $(this).val();
if(enroll.length<=0)
{
$(this).attr("placeholder","Enrollment can not be blank");
$(this).attr("style","color:red;");
return 0;
}
else if(enroll.length>12 || enroll.length<12) {
$(this).attr("style","color:red;");
return 0;
}
else if(enroll.length= =12)
{
var reg=/^[0-9][0-9]{11}$/;
if(reg.test(enroll))
{
$(this).attr("style","color:Black;");
return 1;
}
else
{
$(this).attr("style","color:red;");
return 0;
}
});
}
function Val_mb()
{
$("#mobile").keyup(function(){
var mob=$(this).val();
if(mob.length<=0)
{
$(this).attr("placeholder","Mobile Number can not be blank");
$(this).attr("style","color:red;");
return 0;
}
else if(mob.length>10 || mob.length<10)
{
$(this).attr("style","color:red;");
return 0;
}
else if(mob.length==10)
{
var reg = /^[0-9][0-9]{9}$/;
if(reg.test(mob))
{
$(this).attr("style","color:Black;");
return 1;
}
else
{
$(this).attr("style","color:red;");
return 0;
}
}
});
}
function submitbutton()
{
var z = Val_Fname();
var y = Val_Mname();
var x = Val_Lname();
var w = Val_em();
var v = Val_en();
var u = Val_mb();
if(z==1 && y==1 && x==1 && w==1 && v==1 && u==1)
{
$("#subs_btn").attr("disabled",false);
$("#subs_btn").attr("style","background:rgba(0, 186, 107, 0.71);");
}
else
{
$("#subs_btn").attr("disabled",true);
$("#subs_btn").attr("style","background:grey;");
}
}
submitbutton();
$("#subs_btn").click(function(){
$("#reg_form").submit();
});
});
form
{
\t display:block;
\t width:35%;
\t margin:10em auto;
\t text-align:center;
\t box-shadow:0px 4px 8px #818080;
\t background:#eee;
\t border-top-right-radius:7px;
\t border-top-left-radius:7px;
\t font-family:calibri;
\t padding:1em;
\t overflow:hidden;
}
form input[type="button"]
{
\t width: 66.2%;
\t padding: .7em;
\t background: rgba(0, 186, 107, 0.71);
\t border: none;
\t color: #fefefe;
\t cursor: pointer;
\t margin-top: 1em;
\t font-size: 1.2em;
\t text-shadow:0px 0px 10px black;
}
form input[type="reset"]
{
\t width:20%;
\t padding: .7em;
\t background: rgba(89, 93, 91, 0.7);
\t border: none;
\t color: #fefefe;
\t cursor: pointer;
\t margin-top: 1em;
\t font-size: 1.2em;
\t text-shadow:0px 0px 10px black;
}
form input[type="text"]
{
\t margin:.3em;
\t width:40%;
\t padding:.3em;
}
form h4
{
\t display:block;
\t background:#757575;
\t margin:-.8em;
\t margin-bottom:0.6em;
\t padding:.7em;
\t font-size:1.5em;
\t color:#fffcfc;
\t text-shadow:0px 0px 10px black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>JS Form Validation</title>
</head>
<body>
\t <form method="get" id="reg_form">
\t \t <h4>Student Registration Form</h4>
\t \t <input type="text" name="f_name" id="f_name" placeholder="First Name">
\t \t <input type="text" name="m_name" id="m_name" placeholder="Middle Name">
\t \t <input type="text" name="l_name" id="l_name" placeholder="Last Name">
\t \t <input type="text" name="email" id="email" placeholder="Email">
\t \t <input type="text" name="enroll" id="enroll" placeholder="Enrollment No">
\t \t <input type="text" name="mobile" id="mobile" placeholder="Mobile">
\t \t <input type="button" id="subs_btn" value="Subscribe">
\t \t <input type="Reset" name="Reset" id="Reset" value="Reset">
\t </form>
</body>
</html>
答
我添加了一个评论,但它不会适合在这一领域。
因此,这里是你现在所遇到的问题。
function Val_mb()
{
$("#mobile").keyup(function(){
var mob = $(this).val();
if(mob.length<=0)
代码的这一部分都需要一个调用函数以及每次按下按钮时的按键触发器。所以让我们把它们分开。而不是像这样使用fucntion Val_mb()
。
定义这个全局变量,在这里你的“文件准备部分”然后去掉上面的部分是这样的。
$( “#移动”)。KEYUP(函数(){ 如果(一切正常){ 设置全局变量设置为true或false } }
这里是你的函数
function Val_mb(){
return that global variable;
}
我们可以重新安排所有关键up功能和独立的。那么它应该工作。
document ready {
var elementOneBooleanIdentifier = false;
var elementTwoBooleanIdentifier = false;
var elementThreeBooleanIdentifier = false;
var elementFourBooleanIdentifier = false;
$("elementOneidentifier").keyup(function(){
if (element 1 have the right values)
set its boolean identifier to true or false;
});
$("elementTwoidentifier").keyup(function(){
if (element 2 have the right values)
set its boolean identifier to true or false;
});
$("elementThreeidentifier").keyup(function(){
if (element 3 have the right values)
set its boolean identifier to true or false;
});
$("elementFouridentifier").keyup(function(){
if (element 4 have the right values)
set its boolean identifier to true or false;
});
functions goes here..
return that elements boolean identifiers
}
希望它有助于 感谢
+0
感谢您的建议,但它不起作用 –
嗨,看看这个答案:http://*.com/questions/18907198/jquery-make-sure-all-form-fields-are-filled和除了接受的答案我会控制您的表单中的每个提交值,每当用户输入字段的东西。如果一切正常。然后启用该按钮。 – 2017-04-06 07:04:03
而不是从keyup事件处理函数内部返回一个值,您需要调用一个函数,传递您想要返回的值。 – Shilly
在你的Jquery方法'Val_en()'方法有一些语法问题,我猜你错误地添加了大括号。由于这可能是它不工作。我编辑了Js的代码部分。现在检查。希望这可以帮助。 – RajeshKdev