【问题标题】:How can i grap the div closest to a input field如何获取最接近输入字段的 div
【发布时间】:2015-06-08 14:48:20
【问题描述】:

如何获取最接近具有特定值的具有相同名称的多个输入字段的 div。

这是我的 HTML:

        <div id="template" class="file-row">

            <input id="id_base64_data" name="base64_data" type="hidden">
            <input id="id_base64_name" name="base64_name" type="hidden">
            <input id="id_base64_content_type" name="base64_content_type" type="hidden">

            <!-- This is used as the file preview template -->
            <div>
                <span class="preview"><img class="thumb-image" data-dz-thumbnail /></span>
            </div>
            <div>
                <p class="name" data-dz-name></p>
                <strong class="error text-danger" data-dz-errormessage></strong>
            </div>
            <div>
                <p class="size" data-dz-size></p>
                <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
                    <div class="progress-bar progress-bar-success" style="width:0%;" data-dz-uploadprogress></div>
                </div>
            </div>
            <div>
                <button class="btn btn-primary start">
                    <i class="glyphicon glyphicon-upload"></i>
                    <span>Start</span>
                </button>
                <a class="btn btn-primary crop btn-cropper-modal" data-toggle="modal" data-target=".cropper-modal">
                    Crop
                </a>
                <button data-dz-remove class="btn btn-warning cancel">
                    <i class="glyphicon glyphicon-ban-circle"></i>
                    <span>Cancel</span>
                </button>
                <button data-dz-remove class="btn btn-danger delete">
                    <i class="glyphicon glyphicon-trash"></i>
                    <span>Delete</span>
                </button>
            </div>
        </div>
    </div>

Javascript:

$('input[name=base64_name]').map(function(){

    if(this.value == base64_name) {
        console.log('x')
        $(this).closest(".thumb-image").attr('src', url + image);
    }

});

我在控制台中看到 x,但它没有更新我的图像。 但是当我改变时:

$(this).closest(".thumb-image").attr('src', url + image);

到:

$(".thumb-image").attr('src', url + image);

它正在工作,但它会更改具有该 div 名称的所有图像。

我正在使用 Dropzone 插件,所以我不能对 div 名称做太多事情。

任何帮助将不胜感激!

干杯

【问题讨论】:

  • 你需要展示你的 HTML
  • 我们其实不需要看你的js,我们需要看de html...
  • 我已经编辑了我的代码以添加 HTML

标签: javascript jquery dropzone.js


【解决方案1】:

closest 爬上树来查看它自己和它的祖先。您正在寻找的元素是兄弟姐妹的孩子。

$(this).closest(".file-row").find(".thumb-image")

【讨论】:

  • 这会更新我的第一行,而不是接受我的 if 语句的行。
  • 所以你没有把它放在 if 语句中?
  • 我做到了,但仍然将我的第一个 div 更改为名称 .thumb-image
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2020-10-14
  • 2016-05-28
相关资源
最近更新 更多