admin管理员组文章数量:1023074
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 ofbutton
issubmit
. 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
3 Answers
Reset to default 5A 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.
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 ofbutton
issubmit
. 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
3 Answers
Reset to default 5A 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.
本文标签: javascriptwhy does next() give me 39undefined39Stack Overflow
版权声明:本文标题:javascript - why does .next() give me 'undefined' - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745577502a2157110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论