jQuery触发器仅在第一次出现 [英] jQuery trigger only occurs first time

查看:106
本文介绍了jQuery触发器仅在第一次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多次点击 #input 会上传一个文件。

Clicking #input multiple times will upload a file each time.

点击 #button 反过来触发 #input 只会在第一次上传文件(如果 #input则不会先点击。但是,我会打开文件选择器浏览器,只是不上传文件。

Clicking #button which in turn triggers #input will only upload a file the first time (and not at all if #input was first clicked). I will, however, open the file selector browser, just not upload the file.

为什么会这样,如何更改它以允许多次点击它?

Why is this, and how do I change it to allow it to be clicked multiple times?

PS。为什么我希望这样做而不只是使用 #input ?我正在使用jQueryUI对话框并希望其中一个按钮启动输入。

PS. Why I wish to do so instead of just using an #input? I am using jQueryUI Dialog and wish one of the buttons to initiate the input.

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>My profile</title>
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui.min.js"></script>
        <script type="text/javascript" src="js/jquery.fileupload.js"></script>
        <script type="text/javascript"> 
            $(function(){
                $('#button').click(function(){uploader.trigger('click');});
                var uploader=$('#input').fileupload( {
                    url: 'upload.php',
                    formData: {user_id:50},
                    dataType: 'json',
                    }
                );
            });
        </script>
    </head>
    <body>
        <button id="button">Click</button>
        <input id="input" type="file" name="name">
    </body>
</html>


推荐答案

据我所知,应该有变化按照您首次初始化变量 uploader 的顺序,以及在$ code上注册点击事件的位置>#键。

As far as I think, there should be a change in the order where you first initialize the variable uploader and where you register the click event on the #button.

更新

同时,启动上传者变量点击 #button 每次都应解决问题:

Meanwhile, initiating the uploader variable upon clicking #button each time should solve the problem:

$(function(){
            $('#button').click(function(){
                var uploader=$('#input').fileupload( {
                url: 'upload.php',
                formData: {user_id:50},
                dataType: 'json',
                });
                uploader.trigger('click');
             });
        });

这篇关于jQuery触发器仅在第一次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆