本文介绍了覆盖<输入类型=“文件">价值属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以覆盖标签中的 value 属性吗?我的 JavaScript 代码

It's possible to override value property in tag? My code in JavaScript

var element = document.createElement('input');
element.type = 'FILE';
element.__defineGetter__('value',function(){ return 'ololo'; });
alert(element.value);

它显示空字符串.我也尝试用原型覆盖价值"

It show empty string. Also I try override 'value' with prototype

function BPFILEINPUT(value)
{
    this.value = value;
}
BPFILEINPUT.prototype = element;
var myFileInput = new BPFILEINPUT('ololo');
alert(myFileInput.value);

它可以工作,但会崩溃

form.appendChild(myFileInput);

我尝试通过 4shared.com 上传表单的测试

I try pass test for upload form on 4shared.com

var uplElems = aUploadForm.elements;


   for (var i = 0; i < uplElems.length; i++) {
       var currentUploadElement = uplElems[i]
       if (currentUploadElement.type == 'file') {
           if (currentUploadElement.value == '') {
               // skip
           } else {
               if (!checkFileLength(currentUploadElement)) {
                  return false;
               }
               filesToUpload += getNumberOfSelectedFiles(currentUploadElement);
           }
       }
   }
   if (filesToUpload < 1) {
       alert('You didn\'t select any file to upload yet.');
       return false;
 }

更新

我使用 UIWebView.

I use UIWebView.

推荐答案

AFAIK,由于 ,您无法以编程方式设置 的值安全原因.用户必须明确点击它才能设置其值.

AFAIK, you can't set the value of <input type="file"> programmatically because of security reasons. A user has to explicitly click on it to set its value.

这篇关于覆盖<输入类型=“文件">价值属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 11:13