Jquery 空字段消息显示几秒钟
我是jQuery新手,我正试图通过一个教程来理解它。我从登记表验证开始。我制作了一个脚本,它工作得很好,但消息显示了几秒钟。我希望在用户再次填写该字段之前显示该消息Jquery 空字段消息显示几秒钟,jquery,Jquery,我是jQuery新手,我正试图通过一个教程来理解它。我从登记表验证开始。我制作了一个脚本,它工作得很好,但消息显示了几秒钟。我希望在用户再次填写该字段之前显示该消息 <!DOCTYPE html> <html> <head> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> <meta content="utf-8" http-equiv="encoding"
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<style>
p { color:red; margin:4px; }
b { color:blue; }
</style>
<script src="jquery-latest.js"></script>
<script src="jquery.js"></script>
</head>
<body>
<p></p>
<form id="form1">
Name<input type="text" name="fname" id="fname">
<input type="submit" value="submit" id="submit">
</form>
<script>
$(document).ready(function(){
//var msg=$("p").text("Are you sure?");
$("#form1").click(function(){
if ($('#fname').val()=="") {
//$("#ms").show("slow");
$("p").html("Are you sure?");
//$(this).show("Are you sure?");
}
});
});
</script>
</body>
</html>
p{颜色:红色;边距:4px;}
b{颜色:蓝色;}
名称
$(文档).ready(函数(){
//var msg=$(“p”).text(“您确定吗?”);
$(“#表格1”)。单击(函数(){
如果($('#fname').val()=“”){
//美元(“#ms”)。显示(“慢”);
$(“p”).html(“你确定吗?”);
//$(这个).show(“你确定吗?”);
}
});
});
您好,请做一件事,用您的代码替换下面的代码
<script>
$(document).ready(function(){
//var msg=$("p").text("Are you sure?");
$("#submit").click(function(){
if ($('#fname').val()=="")
{
//$("#ms").show("slow");
$("p").html("Are you sure?");
return false;
//$(this).show("Are you sure?");
}
});
});
</script>
$(文档).ready(函数(){
//var msg=$(“p”).text(“您确定吗?”);
$(“#提交”)。单击(函数(){
如果($('#fname').val()=“”)
{
//美元(“#ms”)。显示(“慢”);
$(“p”).html(“你确定吗?”);
返回false;
//$(这个).show(“你确定吗?”);
}
});
});
最好使用
的事件,而不是单击提交。
将脚本更改为:
<script>
$(document).ready(function(){
//var msg=$("p").text("Are you sure?");
$("#form1").submit(function(){
if ($('#fname').val()=="") {
//$("#ms").show("slow");
$("p").html("Are you sure?");
//$(this).show("Are you sure?");
}
return false;
});
});
</script>
$(文档).ready(函数(){
//var msg=$(“p”).text(“您确定吗?”);
$(“#form1”).submit(函数(){
如果($('#fname').val()=“”){
//美元(“#ms”)。显示(“慢”);
$(“p”).html(“你确定吗?”);
//$(这个).show(“你确定吗?”);
}
返回false;
});
});
在函数末尾使用返回false
$("#form1").submit(function(){
if ($('#fname').val()=="") {
//$("#ms").show("slow");
$("p").html("Are you sure?");
//$(this).show("Are you sure?");
}
return false;
});
对于第二个问题,请使用keyup
$('#fname').keyup(function(){
if(empty($(this).val()))
$("p").show();
else
$("p").hide();
});
然而。。我建议您使用一些jquery,而不是编写自己的…您应该使用$('#form1').submit(function(){})
而不是$(“#提交”)。单击(函数(){})代码>如果教程使用单击
事件,搜索另一个事件,这会教你坏习惯。为什么不使用window.confirm弹出框而不是更改p元素?这样你就不必添加额外的按钮,也可以轻松地为yes no@eicto添加功能:-你是对的,我做了某些更改,现在粘贴了更改代码。请解决问题…@user1817669现在你能告诉我你的问题是什么吗?两者都对,但背后的概念是什么。我想知道你是否使用return如果您将字段留空,则true将提交您的表单。但是,如果您已检查验证及其true,则表示您将字段留空。在这种情况下,您使用return false,则在填写所有字段并再次提交之前,它不会提交您的表单。原因是,您不应以HTML方式提交表单。您需要使用JavaScript进行提交。:)