简体   繁体   English

查找上一个按钮的ID

[英]find ID of previous button

I'm looking to find the id of the previous button. 我正在寻找上一个按钮的ID。 It is pretty far away - lots of table rows, tables, divs, etc. between the target and the button but I thought this would still work: 它很远-目标和按钮之间有很多表行,表,div等,但是我认为这仍然可以工作:

alert( $(this).prevAll("input[type=button]").attr('id') );

Unfortunately this returns alerts 'undefined'. 不幸的是,这将返回警报“未定义”。 Help? 救命?

That kind of lookup might be expensive. 这种查找可能很昂贵。 What about doing a select for all your input[type=button] elements, and traversing that array until you find the element matching your id. 对于所有输入[type = button]元素进行选择,然后遍历该数组直到找到与ID匹配的元素,该怎么办? Then you can simply reference the array index - 1 to get your answer. 然后,您可以简单地引用数组索引-1来获得答案。

function getPrevInput(elem){
    var i = 0,
        inputs = document.getElementsByTagName('input'),
        ret = 'Not found';
    while(inputs[i] !== elem || i >= inputs.length){
        if(inputs[i].type === 'button'){
            ret = inputs[i];
        }
        i++;
    }
    return (typeof ret === 'string') ? ret : ret.id;
}

That probably isn't the most efficient solution, but it's the only one I can think of. 那可能不是最有效的解决方案,但这是我能想到的唯一解决方案。 What it does is goes through all the input elements and finds the one right before the one you passed into the function. 它的作用是遍历所有input元素,并在传递给函数的元素之前找到一个。 You can use it like this, assuming you're calling it correctly and this is the input element: 您可以像这样使用它,假设您正确地调用了它, this就是输入元素:

getPrevInput(this);

Demo 演示版

Is the previous button a sibling of the current button? 前一个按钮是当前按钮的同级按钮吗? If not, prevAll() won't work. 否则, prevAll()将不起作用。 The description of prevAll() : prevAll()的描述:

Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector. 获取匹配元素集中每个元素的所有先前同级,可以选择由选择器过滤。

Depending on your DOM structure, you can use a combination of parents() and then followed by find() . 根据您的DOM结构,您可以结合使用parents()和,然后跟随find()

This function looks up all input[type=button] elements and uses the jQuery index function to find your current element in this group. 此函数查找所有input[type=button]元素,并使用jQuery index函数在该组中查找当前元素。 If it could be found and there is a previous element it is returned. 如果可以找到它并且存在上一个元素,则将其返回。

$.fn.previousElem = function(lookup){
    var $elements = $(lookup),
        index = $elements.index(this);
    if(index > 0){
       return $elements.eq(index-1)
    }else{
      return this;
    }
}

HTML: HTML:

<div><div><div><div>
    <input type=button id=1 value=1 />
</div></div></div></div>

<div><div><div><div>
    <input type=button id=2 value=2 />
</div></div></div></div>

JS: JS:

alert ($("#2").previousElem('input[type=button]').attr('id'))

http://jsfiddle.net/SnScQ/1/ http://jsfiddle.net/SnScQ/1/

Here's a different version of Amaan's code, but jqueryfied and his solution wasn't looking for a button. 这是Amaan代码的不同版本,但是使用jqueryfied时,他的解决方案不是在寻找按钮。 The key to the solution is that jQuery returns the elements in document order, as do document.getElementsByTagName and similar functions. 解决方案的关键是jQuery按照document.getElementsByTagName和类似函数的顺序返回元素。

var button = $('#c');
var prevNode;
$("input[type=button]").each(function() {
    if (this == button[0]) {
      return false;
    }
    prevNode = this;
});
alert(prevNode && prevNode.getAttribute('id'));

http://jsfiddle.net/crFy6/ http://jsfiddle.net/crFy6/

have you tried .closest? 您尝试过.closest吗? ... ...

alert( $(this).closest("input[type=button]").attr('id') );

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

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