简体   繁体   English

如何获得一个具有jquery .data('index')的元素?

[英]How to get an element that has a jquery .data('index') applied to it?

I am setting an element's data attribute onclick of it to the current index, of the element... for example, take the following html: 我正在将元素的数据属性onclick设置为元素的当前索引,例如,使用以下html:

<ul>
    <li class="get-me">
        <a href="javascript:void(0);" class="alink">Click Me</a> <input type="text" value="" name="name1" />
    </li>
    <li class="get-me">
        <a href="javascript:void(0);" class="alink">Click Me</a> <input type="text" value="" name="name2" />
    </li>
    <li class="not-me">
        <input type="text" value="A value" name="aName" readonly />
    </li>
    <li class="get-me">
        <a href="javascript:void(0);" class="alink">Click Me</a> <input type="text" value="" name="name3" />
    </li>
</ul>

Now, onclick, of the a.alink links I add data('index') to each input sibling... example: 现在,在a.alink链接的onclick上,我向每个输入同级中添加data('index') ...示例:

jQuery(document).ready(function($) {
    $('.alink').on('click', function(event) {
        event.preventDefault();
        var $this = $(this),
            $input = $this.next('input');

        if ($input.length)
        {
            $('.alink').each(function(i, obj) {
                if (obj == $this.eq(0))
                {
                    $(obj).next('input').data('index', i);
                    return false;
                }
            });
        }
    });
});

Now the order of these li's can change, after the links have been clicked on, but the jQuery.data stays the same, so wondering if it's possible to get input element that has .data('index') == 1 somehow, as a jQuery object element (like $(this) )? 现在,在单击链接后,这些li的顺序可以更改,但是jQuery.data保持不变,因此想知道是否有可能以某种方式获取具有.data('index') == 1输入元素,例如jQuery对象元素(例如$(this) )? I know that it's possible if the data attribute existed, but this is not the case as it's being set in code instead. 我知道有可能存在data属性,但事实并非如此,因为它是在代码中设置的。 And I'm wondering if this can be selected directly instead of looping through all input elements and checking their .data('index') properties. 我想知道是否可以直接选择它,而不是遍历所有input元素并检查其.data('index')属性。

EDIT 编辑

So I have split this into a function here: 所以我在这里将其拆分为一个函数:

function inputFilter(selector, index)
{
    var obj = null;

    if (selector.trim() !== '')
    {
        obj = $(selector).filter(function() {
            return $(this).data('index') == index;
        });
    }
    return obj;
}

But trying to use it returns all input elements: 但是尝试使用它会返回所有输入元素:

var $object = inputFilter('input', 1);

What am I doing wrong? 我究竟做错了什么?

Use a .filter function that looks for the data you want. 使用.filter函数查找所需的数据。

$('input').filter(function() {
    return $(this).data('index') == '1';
});

 function inputFilter(selector, index) { var obj = null; if (selector.trim() !== '') { obj = $(selector).filter(function() { return $(this).data('index') == index; }); } return obj; } $("#input1").data("index", 1); $("#input2").data("index", 3); console.log(inputFilter('input', 1).map(function() { return this.id; }).get()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="input1"> <input id="input2"> <input id="input3"> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM