简体   繁体   English

jQuery最近的()不适用于不同div中的元素

[英]Jquery closest() doesn't work on elements within different div

I'm trying to use closest() to get <input> element which is in different div. 我正在尝试使用closest()获取不同div中的<input>元素。 Here's the markup: 这是标记:

<div class="row">
    <div class="span8">
        <input type="text" disabled="disabled" id="image-name">
    </div>
    <div class="span4">
        <input type="file">
    </div>
</div>

Here's the script 这是剧本

$(document).ready(function(){
    $("input:file").change(function (){
       var fileName = $(this).val();
       //Put the file name inside the disabled <input>
       $(this).closest('input:disabled').val(fileName);
    });
});

It doesn't do anything though. 它什么也没做。 I tried changing input:disabled to #image-name but still doesn't work. 我尝试将input:disabled更改为#image-name但仍然无法正常工作。

Any solution? 有什么办法吗?

Thanks 谢谢

If you have an id on the other INPUT element, why are you using the closest function? 如果在另一个INPUT元素上有一个ID,为什么要使用最接近的函数? Why not just $('#image-name')? 为什么不只是$('#image-name')? The closest method does not work the way you think. 最接近的方法不符合您的想法。 closest always goes up the DOM tree until it finds a match. 最接近的总是在DOM树上向上移动,直到找到匹配项。

Based on the comment to my initial suggestion, you could get DRY by using the more fancy versions of the jQuery event binders: 根据我最初建议的评论,您可以通过使用更高级的jQuery事件绑定程序来获得DRY:

function handler(e, args) {
    $(e.data.elem).val(fileName);
}

$('input:file').bind('change', { elem: '#image-name' }, handler);
$('#other-input').bind('change', { elem: '#other-field' }, handler);

reusable event handler parameterized using event data constructs. 使用事件数据构造参数化的可重用事件处理程序。

Because closest travels upwards, not sideways. 因为closest向上移动,而不是侧向移动。 Just to add, it starts matching the current element as well. 只是添加,它也开始匹配当前元素。

A more generic way is to use closest to find the common ancestor, which is .row , and find the disabled text box from there. 一种更通用的方法是使用closest来查找公共祖先,即.row ,然后从那里find禁用的文本框。 You could also do parent , prev and children , assuming the HTML is always that way. 假设HTML始终是这种方式,您也可以做parentprevchildren

Use closest to get to the root parent , then use find. 使用closest到根父级,然后使用查找。

$(this).closest('.row').find('input[disabled]').val(fileName);

Try this 尝试这个

Just now noticed your input has ID so you could just do $('#image-name').val(fileName) 刚才注意到您的输入具有ID,因此您可以执行$('#image-name').val(fileName)

You can use: 您可以使用:

$(this).closest('.row').find('input:disabled').val(fileName);

since closest() will traverse up through its ancestors in the DOM tree. 因为closest()将遍历其在DOM树中的祖先。

<a>在 a 内不起作用</a><div></div><div id="text_translate"><p>我有一个具有三种状态的按钮(三个不同的图像),除了按钮的主要 function 外,它可以工作并且看起来很棒 - 链接到其他文档:)</p><p> 当它被点击时,什么也没有发生。 我需要使用 javascript 或&lt;input&gt;或&lt;button&gt;吗?</p><p> 我已经用&lt;button&gt;标签试过了,但是这三种状态都不起作用。</p><p> 而且&lt;a id="backbutton"&gt; ... 也不起作用。</p><pre> #backbutton{ width: 100px; height:100px; background-image: url('../obrazky/fs/back_up100.png'); } #backbutton:hover{ background-image: url('../obrazky/fs/back_hover100.png'); } #backbutton:active{ background-image: url('../obrazky/fs/back_down100.png'); } &lt;div id="backbutton"&gt;&lt;a href="index.html"&gt;&lt;/a&gt;&lt;/div&gt;</pre></div> - <a> doesn't work within a <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jquery nearest()不起作用 - jquery closest() doesn't work jQuery最近和查找似乎不起作用 - Jquery closest and find doesn't seem to work jQuery最近的()remove() <a>不能正常工作吗?</a> - jQuery closest() remove() on <a> doesn't work? 最近的jquery不想工作 - jquery closest doesn't want to work <a>在 a 内不起作用</a><div></div><div id="text_translate"><p>我有一个具有三种状态的按钮(三个不同的图像),除了按钮的主要 function 外,它可以工作并且看起来很棒 - 链接到其他文档:)</p><p> 当它被点击时,什么也没有发生。 我需要使用 javascript 或&lt;input&gt;或&lt;button&gt;吗?</p><p> 我已经用&lt;button&gt;标签试过了,但是这三种状态都不起作用。</p><p> 而且&lt;a id="backbutton"&gt; ... 也不起作用。</p><pre> #backbutton{ width: 100px; height:100px; background-image: url('../obrazky/fs/back_up100.png'); } #backbutton:hover{ background-image: url('../obrazky/fs/back_hover100.png'); } #backbutton:active{ background-image: url('../obrazky/fs/back_down100.png'); } &lt;div id="backbutton"&gt;&lt;a href="index.html"&gt;&lt;/a&gt;&lt;/div&gt;</pre></div> - <a> doesn't work within a <div> jQuery中的Closest()在相同级别上不起作用 - Closest() in jQuery doesn't work when at the same level jQuery的最接近在IE8 / 9中不起作用 - jQuery's closest doesn't work in IE8/9 (select) 事件不适用于 div 元素 - (select) event doesn't work for div elements jQuery最近获取父Div元素的属性 - Jquery Closest getting Attributes of Elements of Parent Div jQuery slideUp在已加载slideDown的ajax div中不起作用 - jQuery slideUp doesn't work within ajax div that has loaded with slideDown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM