Radio button value vits jquery
I have a test form where my radio button have text and value.
I'm trying to pass text from label from checked radio button to hidden field name=form[test]
this is my function:
var levels = $('input[name="form[Button]"]:checked + label').map(function() {
return $(this).text();
}).get();
$('input[name="form[test]"]').change( function() {
($(this).val(levels));
});
HTML
<input type="radio" id="Button0" value="0" name="form[Button]">
<label for="Button0">This item is worth £0</label>
<input type="radio" id="Button1" value="100" name="form[Button]">
<label for="Button0">This item is worth £100</label>
<input type="hidden" valu开发者_如何学Pythone="" id="test" name="form[test]">
But my change in jQuery simply dont work.
Many Thanks for your help in advance!
Dom
Try it this way:
$('input[name="form[Button]"]').each(function() {
$(this).data('label', $(this).next('label').text());
});
$('input[name="form[Button]"]').change(function() {
$('input[name="form[test]"]').val($(this).data('label'));
});
$('input[name="form[Button]"]').change(function() {
var level = $('input:radio[name="form[Button]"]:checked + label').text();
$('input[name="form[test]"]').val(level);
});
And you can see a live demo.
This
$('input[name="form[test]"]').change( function() {
($(this).val(levels));
});
means that you only want to change the value of the hidden input when it already changes by some other influence. I think you mean:
var levels = $('input[name="form[Button]"]:checked + label').map(function() {
return $(this).text();
}).get();
$('input[name="form[test]"]').val(levels);
精彩评论