I mocked up some HTML to test this:

<input type="text" id="text" />
<select id="options">
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
</select>

I was using the following JavaScript to alert the type of the select tag, but I kept getting false.

var elem = $("#text");
alert(elem.after().attr('id'));

I also tried using Jquery's prop() method but to no avail, I am just trying to check whether the element that follows the input tag is a select tag...

有帮助吗?

解决方案

You need to use .next() since select is the immediate next sibling of your input with id text:

alert(elem.next().attr('id'));

Your code is not working because .after() is used to

Insert content, specified by the parameter, after each element in the set of matched elements.

其他提示

First you have to use next instead of after() also you have to use type attribute to check if the next element is Select, use prop instead of attr

Live Demo

alert(elem.next().prop('tagName') == 'SELECT');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top