PHP停止表单重新加载显示错误Js
PHP Stop Form Reload Show Error Js
如果"fileToUpload"为空,我想显示我的错误(用js显示错误),如果"fileToUpload"是空,我想停止重新加载页面
<form method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input class='new_post' type="submit" value="Upload Image" name="submit">
</form>
$('.new_post').click(function() {
var get_photo = $('#fileToUpload').get(0).files.length;
// If 0 file show error
if (get_photo == 0) {
openandclose('.error_box' , 'emptyyyy.', 1700);
$('.error_box').focus();
return false;
}
});
向表单添加一个id,如<form method="post" enctype="multipart/form-data" id="myform">
,并在javascript中将检查函数作为回调附加到表单提交事件,而不是像$('#myform').submit()
那样的按钮单击事件。
$('#myform').submit(function() {
var get_photo = $('#fileToUpload').get(0).files.length;
// If 0 file show error
if (get_photo == 0) {
/*
openandclose('.error_box', 'emptyyyy.', 1700);
$('.error_box').focus();
*/
alert("EMPTY!!");
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" enctype="multipart/form-data" id="myform">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input class='new_post' type="submit" value="Upload Image" name="submit">
</form>
这段代码可能会帮助您。单击按钮后,它会检查是否选择了文件,并相应地返回True/False。
<form method="post" enctype="multipart/form-data" onsubmit="return funccheck();">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input class='new_post' type="submit" value="Upload Image" name="submit"> </form>
<script>
function funccheck() {
var get_photo = $('#fileToUpload').get(0).files.length;
// If 0 file show error
if (get_photo == 0) {
openandclose('.error_box' , 'emptyyyy.', 1700);
$('.error_box').focus();
return false; //stops page loading
}
return true; // initiates page loading
});
</script>
试试这个:
$('.new_post').click(function(e) { var get_photo = $('#fileToUpload').get(0).files.length; // If 0 file show error if (get_photo == 0) { openandclose('.error_box' , 'emptyyyy.', 1700); $('.error_box').focus();
e.preventDefault();
return false; } });
变量e是事件,如果没有文件,函数preventDefault将阻止表单提交。
相关文章:
- 如何在生成下载文件时显示加载动画
- 如何在谷歌字体加载时显示加载图像
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 如何用ajax实现加载显示
- 使用javascript加载/显示图像并获得%下载
- 谷歌地图不会加载/显示KML文件
- 我的图片不是使用惰性加载显示的
- javascript d3可视化不加载/显示,直到点击HTML输入
- 导航标签动态加载/显示
- jQuery Select2远程数据加载:显示选项而不是占位符
- Console.log一些东西一旦异步Facebook喜欢按钮加载(显示)
- PHP停止表单重新加载显示错误Js
- 预加载显示/隐藏分区
- ReCaptcha不会在IE中加载/显示,但在Chrome和safari中可以完美运行
- dom事件——通过JavaScript加载显示一个又一个元素
- jQuery延迟加载-显示的问题:无
- Json.数据没有正确加载/显示
- 在每个页面加载显示不同的图像
- 如何检测PDF数据对象是否在html对象中加载/显示/有效
- Jquery添加页面加载显示请求的标签功能