jQuery $('#fileupload').fileupload 函数没有调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14788066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:55:43  来源:igfitidea点击:

$('#fileupload').fileupload function is not calling

jqueryblueimp

提问by Manish Sahu

I am configuring blueimp to upload image on click to post button. My problem is it is not calling the $('#fileupload').fileuploadfunction. I have included following javascript files. The following is my code please see what is the problem.

我正在配置 blueimp 以通过单击发布按钮上传图像。我的问题是它没有调用该$('#fileupload').fileupload函数。我已经包含了以下 javascript 文件。以下是我的代码,请看看有什么问题。

<head>
    <script  type="text/javascript" src="../../js/jquery.min.js"></script>
    <script  type="text/javascript" src="../../js/jquery-ui.js"></script>
    <script  type="text/javascript" src="../../js/jQuery-File-Upload-master/js/jquery.fileupload.js"></script>
    <script type="text/javascript" src="../../js/jQuery-File-Upload-master/js/vendor/jquery.ui.widget.js"></script>
    <script type="text/javascript" src="../../js/jQuery-File-Upload-master/js/jquery.iframe-transport.js"></script>
    <script  type="text/javascript">
        $('#fileupload').fileupload({
            dataType : 'json',
            url : "ownmessages",
            add: function (e, data) {
                $('#post') .click(function () {
                    $('#post').text('Uploading...');
                    data.submit();
                });
            },
            done: function (e, data) {
                $('#post').text('Upload finished.');
            }
        });
    </script>
</head>
<body>
    <s:form action="ownmessages" enctype="multipart/form-data" method="post">
        <s:textarea rows="2" cols="40" name="message" id="message1"></s:textarea><br>
        <s:select name="msg_visibility" id="msg_visibility" list="#{'public':'Public', 'friends':'Friends','me':'Me only'}" value="public"/>
        <input id="fileupload" type="file" name="user_post_image[]"  data-url="ownmessages"/>
        <input type="button" value="Post" id="post" />
    </s:form>
</body>

回答by Seain Malkin

You need to wait for the document to finish loading before you try and select any elements. The jQuery .ready()method waits for the Document Object Model (DOM) to finish loading and then runs your script.

在尝试选择任何元素之前,您需要等待文档完成加载。jQuery.ready()方法等待文档对象模型 (DOM) 完成加载,然后运行您的脚本。

$(document).ready(function() {
  $('#fileupload').fileupload({
        dataType : 'json',
        url : "ownmessages",
        add: function (e, data) {
            $('#post') .click(function () {
                $('#post').text('Uploading...');
                data.submit();
            });
        },
        done: function (e, data) {
            $('#post').text('Upload finished.');
        }
    });
});

Instead of using .ready()you can also just pass a function to jQuery.

除了使用,.ready()您还可以将函数传递给 jQuery。

$(function() {
    // will run after DOM is loaded
});

回答by mikakun

first problem :

第一个问题:



<script  type="text/javascript">
   $(document).ready(function() {
    $('#fileupload').fileupload({
        dataType : 'json',
        url : "ownmessages",
        add: function (e, data) {
            $('#post') .click(function () {
                $('#post').text('Uploading...');
                data.submit();
            });
        },
        done: function (e, data) {
            $('#post').text('Upload finished.');
        }
    });
   });
</script>

that's a beginning but might not be enough...

这是一个开始,但可能还不够......

next for the sake of optimization at least, move your script tags to the bottom

接下来至少为了优化起见,将您的脚本标签移到底部