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"]');
精彩评论