我觉得很奇怪,如果我在表单提交的错误 div 上显示错误,它们会立即消失
我的测试代码
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<form method="post" id="fid" action="#">
<input type="text"><div id="err"></div>
<input type="submit" value="Submit" name="button" />
</form>
<script type="text/javascript">
$(function(){
$('#fid').submit(function(){
$('#err').text('Hello World');
//$('#err').html('Hello World');
});
});
</script>
</body>
</html>
我只是显示一条错误消息,无论用户是否将该字段留空或不只是为了检查我是否成功获得此信息,然后我会去实现全部功能。
请注意:错误困惑出现不到一秒钟,然后自动消失
问候
最佳答案
e.preventDefault() ;停止表单提交操作
$(function(){
$('#fid').click(function(e){
e.preventDefault();
$('#err').text('Hello World');
//$('#err').html('Hello World');
});
});
如果调用该方法,则不会触发事件的默认 Action
关于javascript - jquery 为什么.text 自动隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24204345/