单击div上的任意位置以触发输入

时间:2017-05-31 16:07:43

标签: javascript jquery uploadcare

我试图这样做,当你点击一个特定的div时,它会触发一个输入按钮来打开uploadcare小部件。

以下是有问题的HTML

<div class="col-md-6 image-preview-single">
<img src="" alt="" class="img-responsive card-shadow hidden">
    <div id="logo-upload" class="add-new-short add-new-home-realtor card-shadow-mini btn-file uploader-homicity" style="display:block;text-decoration:none; cursor: pointer;">
        <i class="fa fa-photo"/>
        <input type="hidden" role="uploadcare-uploader" name="logo" data-upload-url-base="" data-images-only="true" data-crop="150x150" data-clearable="true" id="logo-input">
            <div class="uploadcare-widget uploadcare-widget-option-clearable uploadcare-widget-status-ready" data-status="ready">
                <div class="uploadcare-widget-dragndrop-area">
                    Drop a file here
                </div>
                <div class="uploadcare-widget-status uploadcare-widget-circle uploadcare-widget-circle--canvas">
                    <canvas width="56" height="56"/>
                </div>
                <div class="uploadcare-widget-text">
                </div>
                <div tabindex="0" role="button" class="uploadcare-widget-button uploadcare-widget-button-open">Choose an image</div>
                <div tabindex="0" role="button" class="uploadcare-widget-button uploadcare-widget-button-cancel">Cancel</div>
                <div tabindex="0" role="button" class="uploadcare-widget-button uploadcare-widget-button-remove">Remove</div>
            </div> 
    </div>

和Jquery有问题

$( '#logo-upload' ).on( 'click', function() {
    $('input#logo-input').click();
});

这是控制台错误

Uncaught RangeError: Maximum call stack size exceeded
    at RegExp.exec (<anonymous>)
    at n.fn.init (jquery.min.js:2)
    at n (jquery.min.js:2)
    at HTMLDivElement.<anonymous> (uploadcare.js:72)
    at HTMLDivElement.dispatch (jquery.min.js:3)
    at HTMLDivElement.r.handle (jquery.min.js:3)
    at Object.trigger (jquery.min.js:4)
    at HTMLInputElement.<anonymous> (jquery.min.js:4)
    at Function.each (jquery.min.js:2)
    at n.fn.init.each (jquery.min.js:2)

我对此缺少什么?

由于

4 个答案:

答案 0 :(得分:2)

您还需要在输入点击上添加此事件:

$('input#logo-input').on('click', function(e) {
    e.preventDefault();
    e.stopPropagation();
    ...your code
});

您需要stop propagation,因为您创建了无限循环。 当您触发单击输入时,此输入位于logo-upload div内,单击输入将传播到此容器,这将创建无限循环。

答案 1 :(得分:0)

两个选项:

  1. 使用<label>仅使用html激活输入:

    <label for="yourId">Click here<label> <input id="yourId" type="text" />

  2. 使用focus()代替click()stopPropagation()stopPropagation()

    $('#logo-upload' ).on( 'click', function(e) { e.preventDefault(); e.stopPropagation(); $('input#logo-input').focus(); });

答案 2 :(得分:0)

确定,

因此您要打开Uploadcare 对话框,而不是小部件

首先,从页面中删除所有窗口小部件的HTML代码,它由库添加,并且应该在服务文档中。 其次,使用以下代码

int incFuel = 0;
final String FUELBAR = "fuelBar";

SharedPreferences sharedPref;
SharedPreferences.Editor editor;

答案 3 :(得分:0)

就这样做。

var inputSP = input.click(function (e) { e.stopPropagation(); });

dropper.on('click', function (e) {
   e.stopPropagation();   
    // here we first stop propagation then make a call to click event 
   inputSP; 
   input.click();
});