简体   繁体   English

更改时不触发onchange事件

[英]onchange event not firing when changed

I have this in my html, which works: 我在我的html中有这个,它可以工作:

           <input id="File1" type="file" runat="server" onchange="fileUpload(value)"  /> 

But for IE8 and below I have a button to create a new input: type=file It is working and uploads the file. 但是对于IE8及以下版本,我有一个按钮来创建新输入:type = file它正在工作并上传文件。

But I need to send the value to a function first. 但是我需要先将值发送给函数。

            newUploadBox.setAttribute("onchange", "fileUpload(value)");

Now when I go into inspect element, everything looks right. 现在,当我进入检查元素时,一切看起来都正确。

It renders out like the original input. 它像原始输入一样呈现。

Unfortunately, it does not go to the function like the first one does. 不幸的是,它没有像第一个那样进入函数。

I fond some post on here: Dynamically added SELECT element does not fire onchange event in Internet Explorer && onchange with alert not working in ie 我在这里喜欢一些帖子: 动态添加的SELECT元素不会在Internet Explorer中触发onchange事件,并且 && onchange不会在警报中起作用,即

But none are able to help me. 但是没有人能帮助我。

https://jsfiddle.net/satjzr6z/ https://jsfiddle.net/satjzr6z/

Try this code 试试这个代码

newUploadBox.onchange = fileUpload.bind(this, value);

function.prototype.bind - creates a bound function that has the same body as the original function function.prototype.bind-创建一个绑定函数,该函数具有与原始函数相同的主体

Method description 方法说明

I added a onClientClick event handler to check the validation for me instead of doing an onchange event listener. 我添加了onClientClick事件处理程序来为我检查验证,而不是执行onchange事件侦听器。

I used the following function: 我使用了以下功能:

function FileUploadValidate() {
var input;
var thisId;
var fileInput;

input = document.getElementsByTagName("input");

for(i = 0;i < input.length; i++)
{
    if(input[i].getAttribute("type") === "file"){
        thisId = input[i].getAttribute("id"); 
        fileInput = document.getElementById(thisId);
        fileUpload(fileInput.value, thisId)

    }
}

for(i = 0;i < input.length; i++)
{
    if(input[i].getAttribute("type") === "file"){
        thisId = input[i].getAttribute("id"); 
        if(document.getElementById(thisId).style.backgroundColor === "#ff0000"){
            alert("true")
            document.getElementById("decoyBtn").innerText = "Check"
            return
        }else{
            document.getElementById("decoyBtn").style.display = "none"
            document.getElementById("btnSubmit").style.display = "inline"

        }


    }
  } 
}

This will parse through all my file upload input fields. 这将解析我所有的文件上传输入字段。 If one doesnt meet my requirements it will not allow them to upload. 如果不符合我的要求,将不允许他们上传。 It is the best work around I could come up with that would work in IE8. 这是我能想到的最好的工作,它将在IE8中工作。

I also used a decoy button that would basically be my "onClientClick" because I didnt want an accidental upload. 我还使用了一个诱饵按钮,该按钮基本上是我的“ onClientClick”,因为我不想意外上传。

The second for each loop checks the condition of all of them validating and if they do it will take the decoy button off the DOM and bring in the ASP.NET button for upload. 每个循环的第二个命令检查所有条件都经过验证的条件,如果这样做,则将decoy按钮从DOM上移开,并引入ASP.NET按钮进行上传。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM