用于表单验证的jquery keyup函数
我正在试图找出如何将keyup函数嵌入到我目前拥有的代码中。代码运行良好,但我必须在输入字段外单击以执行它 我相信on keyup函数将消除这种行为,使其更加生动。但是我是jquery和java脚本的初学者,所以非常感谢您的帮助用于表单验证的jquery keyup函数,jquery,html,Jquery,Html,我正在试图找出如何将keyup函数嵌入到我目前拥有的代码中。代码运行良好,但我必须在输入字段外单击以执行它 我相信on keyup函数将消除这种行为,使其更加生动。但是我是jquery和java脚本的初学者,所以非常感谢您的帮助 $(document).ready(function (){ validate(); $('#FirstName, #LastName, #PhoneNumber, #Email, #Age, #Profession, #ChooseYourCoursePreferab
$(document).ready(function (){
validate();
$('#FirstName, #LastName, #PhoneNumber, #Email, #Age, #Profession, #ChooseYourCoursePreferableMonth, #AreYouBringingYourLaptop, #hear, #PreferableContactMethod').change(validate);
});
function validate(){
if (
$('#FirstName').val().length > 2 &&
$('#LastName').val().length > 2 &&
$('#PhoneNumber').val().length > 7 &&
$('#Email').val().length > 0 &&
$('#Age').val().length > 0 &&
$('#Profession').val().length > 0 &&
$('#ChooseYourCoursePreferableMonth').val().length > 0 &&
$("input[name='AreYouBringingYourLaptop']:checked").val().length > 0 &&
$('#hear').val().length > 0 &&
$("input[name='PreferableContactMethod']:checked").val().length > 0
)
{ // FILLED
$("input[type=submit]").prop("disabled", false);
$('input[type=submit]').val('Submit Form');
$('input[type=submit]').css('color', '#383838');
$('input[type=submit]').css('background-color', '#63bc46');
$('input[type=submit]').css('font-size','18px');
$('input[type=submit]').css('cursor','pointer');
}else {
// NOT FILLED
$("input[type=submit]").prop("disabled", true);
$('input[type=submit]').val('Fill in all required fields to Submit Form');
$('input[type=submit]').css('color', '#a0a0a0');
$('input[type=submit]').css('background-color', '#f3f3f3');
$('input[type=submit]').css('font-size','15px');
$('input[type=submit]').css('cursor','auto');
}
}您可以使用.keyup()
。我将发布一个例子
$( "input[type=submit]" ).keyup(function() {
validate();
});
jQuery网站上的示例:您可以使用.keyup()
。我将发布一个例子
$( "input[type=submit]" ).keyup(function() {
validate();
});
jQuery网站上的示例:使用.on(“输入”)
这将在您每次按某个输入中的某个键时调用该函数
像这样:
$("#yourinput").on("input", function(){
//validation
});
或者,调用validate()函数
使用.on(“输入”)
这将在您每次按某个输入中的某个键时调用该函数
像这样:
$("#yourinput").on("input", function(){
//validation
});
或者,调用validate()函数
试试这个代码
$('#FirstName, #LastName, #PhoneNumber, #Email, #Age, #Profession, #ChooseYourCoursePreferableMonth, #AreYouBringingYourLaptop, #hear, #PreferableContactMethod').keyup(function() {
validate();
});
希望这将对您有所帮助。尝试此代码
$('#FirstName, #LastName, #PhoneNumber, #Email, #Age, #Profession, #ChooseYourCoursePreferableMonth, #AreYouBringingYourLaptop, #hear, #PreferableContactMethod').keyup(function() {
validate();
});
希望这能对您有所帮助。它起作用了!,这里我给出了类似代码的演示,请检查它:它的工作!,这里我给出了类似代码的演示,请检查它:它的工作!,这里我给出了类似代码的演示,请检查: