我有以下HTML和jquery函数来验证输入。我想在按任意键时隐藏错误消息。那怎么做请帮我写按键功能。谢谢。
$(document).ready(function() {
var allids = [];
$("#artistselect").change(function() {
var id = $(this).children(":selected").attr("id");
allids.push($(this).children(":selected").attr("value"));
var selectid = "#" + id;
var echoid = "#artistecho" + id;
$(echoid).show(300);
$(selectid).hide(0);
$("input[name=artistslist]").val(allids.join(", "));
alert(allids);
});
$(".remove").click(function() {
var removeid = $(this).attr("value");
allids = allids.replace(removeid, '');
$('input[name=artistslist]').val(function(index, value) {
return value.replace(removeid, '');
});
alert(newids);
});
})
size: {
height: 100,
width: 100
}
答案 0 :(得分:1)
$(document).keypress(function(e) {
$('.error').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
<div class="error">please enter value</div>
答案 1 :(得分:0)
$("#FName").focusin(function(){
$(".error").hide();
});
$("#FName").focusout(function(){
if($(this).val() === ""){
$(".error").show();
}else{
$(".error").hide();
}
});
作为一个例子
$(document).ready(function(){
$("#FName").focusin(function(){
$(".error").hide();
});
$("#FName").focusout(function(){
if($(this).val() === ""){
$(".error").show();
}else{
$(".error").hide();
}
});
});
function Validate() {
var isAllValid = true;
$('.error').hide();
$('#error').empty();
$('.form-group').removeClass('has-error');
if ($('#FName').val().trim() == "") {
$('#FName').focus();
$('#FName').siblings('.error').show();
$('#FName').parents('.form-group').addClass('has-error');
isAllValid = false;
}
if ($('#Email').val().trim() != "" || $('#Email').val().trim() =="") {
var expr = /^([a-zA-Z0-9_\-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (!expr.test($('#Email').val().trim())) {
$('#Email').focus();
$('#Email').siblings('.error').show();
$('#Email').parents('.form-group').addClass('has-error');
isAllValid = false;
}
}
return isAllValid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label for="LName" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="FName" placeholder="First Name">
<span class="error">**Please provide First Name...</span>
</div>
</div>
<div class="form-group">
<label for="Email" class="col-sm-2 control-label">Email Address</label>
<div class="col-sm-10">
答案 2 :(得分:0)