我想添加一个onSubmit
事件所有的HTML Forms
验证提交文件大小并阻止其提交javascript
.
问题是我没有id
of the form
,也没有那些file input element
。现在,我怎样才能获得所需的值input
?
可能有几个forms
每一页都有未知的内容id
但每个form
只有一个file input element
. The id
该元素的含量也是未知的。
example form
:
<form onSubmit="checkSize(1000000)" enctype="multipart/form-data" action="changedata.php" method="post" >
<input class="user_upload_file" type="file" name="file" />
<input type="submit" value="submit" />
</form>
和我使用的JavaScript:
function checkSize(max_img_size)
{
//file_input_id is unknown so, the following input could not be defines:
var input = document.getElementById("file_input_id");
// check for browser support (may need to be modified)
if(input.files && input.files.length == 1)
{
if (input.files[0].size > max_img_size)
{
alert("File size is too big. ");
return false;
}
}
return true;
}
另外,我无法更改id
所有的file input
s,确实很费时间。我也不使用jQuery
.
感谢您的想法
通过内在事件属性包含的事件处理程序在它们所附加的元素的上下文中调用,您可以像任何其他变量一样传递它。
onsubmit="checkSize(1000000, this)"
从那里你可以使用标准 DOM 方法,例如getElementsByTagName
and querySelector
随着elements
属性来查找后代元素。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)