最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - why does .next() give me 'undefined' - Stack Overflow

programmeradmin4浏览0评论

why does .next() returns 'undefined'? /

html

<button value="login|basic" class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">

<button value="test|advanced" class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">

<button value="best|4444" class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">

<div id='results'/>

javascript + jQuery

$(document).ready(function(){
    $('button[type=button]').click(function(){
        var params = $(this).val();
        document.getElementById("results").innerHTML+=
          "<BR>"+params.split('|')[0]+" - "
           + params.split('|')[1]+" - "
           + $(this).next().checked;
     });
});

why does .next() returns 'undefined'? http://jsfiddle/radek/sD6JB/2/

html

<button value="login|basic" class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">

<button value="test|advanced" class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">

<button value="best|4444" class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">

<div id='results'/>

javascript + jQuery

$(document).ready(function(){
    $('button[type=button]').click(function(){
        var params = $(this).val();
        document.getElementById("results").innerHTML+=
          "<BR>"+params.split('|')[0]+" - "
           + params.split('|')[1]+" - "
           + $(this).next().checked;
     });
});
Share Improve this question asked May 31, 2011 at 7:06 RadekRadek 11.1k56 gold badges169 silver badges270 bronze badges 5
  • 1 Why do you even have <button type="button"> tags in the first place? Buttons are buttons. – BoltClock Commented May 31, 2011 at 7:09
  • "Buttons are buttons." lol! – sscirrus Commented May 31, 2011 at 7:11
  • @BoltClock The default type attribute of button is submit. Source. – alex Commented May 31, 2011 at 7:15
  • why? bacause of that w3schools./tags/tag_button.asp – Radek Commented May 31, 2011 at 7:15
  • all great answers and withing 2 minutes of posting my question ... – Radek Commented May 31, 2011 at 7:23
Add a ment  | 

3 Answers 3

Reset to default 5

A jQuery object has no checked property.

You can either...

(a) Subscript the native DOM element with [0] or get(0).

(b) Access prop('checked') (>= jQuery 1.6 only.)

(c) Use is(':checked').

That's because you're calling .checked on the jQuery object and not the DOM element. If you want to call .checked you need to get the DOM element out of the jQuery collection.

Simply changing that line to the below will work

$(this).next().get(0).checked

Fixed fiddle : http://jsfiddle/sD6JB/3/

It is undefined because, when you are in the click function $(this) set only holds one element, on which you define the click property. So there is no next sibling in the set.

Explanation:

$('button[type=button]') holds all button elements with type=button attribute. When you define clickhandler on them, you create a function for each, and in that function this will be the element, on which you define the event handler. When you the use $(this) you create a jQuery wrapper set, that contains only this element. .next() is used to select the next sibling in the set, but your set has only one element.

Solution:

$(this).siblings("input").is(":checked")

Btw, using checked is also a problem, but your initial undefined problem's cause is the above. Use .is(":checked") instead.

发布评论

评论列表(0)

  1. 暂无评论