【问题标题】:Click anywhere on a div to trigger an input单击 div 上的任意位置以触发输入
【发布时间】:2017-05-31 16:07:43
【问题描述】:

我正在尝试这样做,当您单击特定 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)

我错过了什么?

谢谢

【问题讨论】:

标签: javascript jquery uploadcare


【解决方案1】:

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

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

您需要stop propagation,因为您创建了无限循环。 当您触发点击输入时,此输入在logo-upload div 内,点击输入会传播到此容器并创建无限循环。

【讨论】:

  • preventDefault 是无害的,但在这里是不必要的; stopPropagation 正在做这项工作。
  • 是的@DanielBeck preventDefault 在这里并不重要,但stopPropagation 是停止无限循环的关键。
【解决方案2】:

两种选择:

  1. 使用&lt;label&gt; 仅通过 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(); });

【讨论】:

    【解决方案3】:

    好的,

    所以您想打开 Uploadcare 对话框,而不是小部件

    首先,从您的页面中删除所有小部件的 HTML 代码,它是由库添加的,应该在服务文档中。 二、使用如下代码

    $('#logo-upload').on('click', function() {
        uploadcare.Widget(input_selector).openDialog();
    });
    

    【讨论】:

      【解决方案4】:

      就这样做吧。

      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();
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 2011-12-16
        相关资源
        最近更新 更多