onchange没有在IE上工作,但在Chrome和FF上完美开启

时间:2017-01-25 07:17:14

标签: javascript html laravel cross-browser onchange

我遇到onchange事件的问题。它适用于Chrome和FF,但不适用于IE。这是代码:

<div class="panel-body panel-default upload">
    <div class="form-group upload">
        {!! Form::open(array('url'=>'scriptUpload/upload','method'=>'POST', 'files'=>true, 'class' => 'fluid ui action labeled input')) !!}
        <label for="project" class="col-sm-6 control-label">Elementary script</label>

            <div class="control-group">
                <div class="controls input">
                    <input id="files" type="text" class="input upload" readonly>
                        <label class="col-sm-5">
                            <span class="btn btn-primary upload">
                                Browse&hellip; <input name='elementaryScript[]' id='elementaryScriptId' type="file" style="display: none;" multiple onchange="isUploadPossible(this)">
                            </span>
                        </label>

                </div>
            </div>
        <div id="success">
            {!! Form::button('Upload', array('id' => 'submitButton', 'class'=>'btn btn-primary upload', 'onClick' => 'fileExists()', 'disabled'=>'true')) !!}
            {!! Form::close() !!}
        </div>
    </div>  
</div>

以下是JS函数:

function isUploadPossible(elem) {
    if (elem.value == "")
        elem.getRootNode().getElementsByTagName('button').submitButton.disabled = true;
    else
        elem.getRootNode().getElementsByTagName('button').submitButton.disabled = false;

}

基本上,当用户选择文件时,它应该将上传按钮的disabled属性设为false

我在这里读到它应该更改为onclick但我不能,因为在用户选择要上传的文件并且onclick将被解雇后必须被触发之前。

编辑:我碰巧遇到了这个错误:

Object doesn't support property or method 'getRootNode'

1 个答案:

答案 0 :(得分:1)

好的,到目前为止看来getRootNode只是supported by Chrome and Opera

找到一种(非常)脏的方法,但适用于IE,Chrome和FF:

elem.parentElement.parentElement.parentElement.parentElement.parentElement.getElementsByTagName('button').submitButton;