开发者

jquery selecting next element from $(this) position

开发者 https://www.devze.com 2023-04-03 03:27 出处:网络
I have the following snipped of my page: <div class=\"radio-group\"> <input type=\"radio\" name=\"group1\" checked=\"checked\" id=\"option1\" value=\"option1\"/>

I have the following snipped of my page:

<div class="radio-group">
<input type="radio" name="group1" checked="checked" id="option1" value="option1"/>
<label for="option1">option1</label>
<input type="radio" name="group1" id="option2" value="option2"/>
<label for="option2">option2</label>
<input type="radio" name="group1" id="option3" value="option3"/>
<label for="option3">option3</label>
<input type="radio" name="group1" id="option4" value="option4"/>
<label for="option4">option4</label>
</div>

and this javascript code to select the hidden 开发者_运维百科input after a click in the radios:

$("input[type='radio']").live("change", function () {        
    var el = this.parentNode.nextElementSibling;
    el.value = this.value;
});

I want to write the line

var el = this.parentNode.nextElementSibling;

in a jQuery way. Thanks.


var el = $(this).parent().next().find("input[type='radio']");
el.val(this.value);

very easy. :)

Edit

lol didn't pay enough attention to the markup. :P fixed.


$("input[type='radio']").live("change", function () {        
    var el = $(this).parent().next();
    el.val(this.value);
});


var el = this.parentNode.nextElementSibling;

translates to

var $el = $(this).parent().next();


use jquery's this, also note that you can filter the next selector (in case something goes wrong with your code)

var el = $(this).next('input[type="hidden"]');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号