简体   繁体   English

为什么.next() 给我“未定义”

[英]why does .next() give me 'undefined'

why does.next() returns 'undefined'?为什么.next() 返回“未定义”? http://jsfiddle.net/radek/sD6JB/2/ http://jsfiddle.net/radek/sD6JB/2/

html 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 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;
     });
});

A jQuery object has no checked property. jQuery object 没有已checked的属性。

You can either...你也可以...

(a) Subscript the native DOM element with [0] or get(0) . (a) 使用[0]get(0)为原生 DOM 元素下标。

(b) Access prop('checked') (>= jQuery 1.6 only.) (b) 访问prop('checked') (>= jQuery 仅 1.6。)

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

That's because you're calling .checked on the jQuery object and not the DOM element.那是因为您在 jQuery object 而不是 DOM 元素上调用.checked If you want to call .checked you need to get the DOM element out of the jQuery collection.如果你想调用.checked ,你需要从 jQuery 集合中取出 DOM 元素。

Simply changing that line to the below will work只需将该行更改为以下内容即可

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

Fixed fiddle: http://jsfiddle.net/sD6JB/3/固定小提琴: http://jsfiddle.net/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. 它是 undefined的,因为当您在 click时 function $(this)集仅包含一个元素,您可以在该元素上定义 click属性。 So there is no next sibling in the set. 所以集合中没有下一个兄弟。

Explanation: 解释:

$('button[type=button]') holds all button elements with type=button attribute. $('button[type=button]')包含所有具有 type=button属性的 button元素。 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. 当您在它们上定义 clickhandler 时,您将为每个创建一个 function,并且在该 function this将是您定义事件处理程序的元素。 When you the use $(this) you create a jQuery wrapper set, that contains only this element. 当您使用 $(this)时,您将创建一个仅包含此元素的 jQuery 包装器集。 .next() is used to select the next sibling in the set, but your set has only one element. .next()用于 select 集合中的下一个兄弟,但您的集合只有一个元素。

Solution: 解决方案:

$(this).siblings("input").is(":checked") $(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.使用.is(":checked")代替。

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

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