我有一个表单上传图像.我想禁用提交按钮,直到用户选择要上传的图像.我想用jQuery做.目前,我有一个JavaScript,阻止用户在提交表单时多次提交表单.将这个功能与新的功能结合起来真是太好了.
这是我现在所做的:
<script type="text/javascript"> function submitonce(theform) { //if IE 4+ or NS 6+ if (document.all || document.getElementById) { //screen thru every element in the form,and hunt down "submit" and "reset" for (i = 0; i < theform.length; i++) { var tempobj = theform.elements[i] if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset") //disable em tempobj.disabled = true } } } </script> <form name="form" enctype="multipart/form-data" method="post" action="upload.PHP" onSubmit="submitonce(this)"> <input type="file" name="my_field" value="" /> <input type="submit"> </form>
解决方法
以下内容似乎在Chrome和Firefox(Ubuntu 10.10)中可靠运行,目前我无法查看其他平台:
jQuery的
$(document).ready( function(){ $('input:file').change( function(){ if ($(this).val()) { $('input:submit').attr('disabled',false); // or,as has been pointed out elsewhere: // $('input:submit').removeAttr('disabled'); } } ); });
HTML
<form action="#" method="post"> <input type="file" name="fileInput" id="fileInput" /> <input type="submit" value="submit" disabled /> </form> <div id="result"></div>