当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to pass value of radio button to another form field using jquery?

问题描述:

I want to pass the value either no or 1 to the input name="auth[]" below.

<td> send

<input type="radio" name="authorized[]'.$c.'" id="send'.$c.'"value="1" checked>

</td>

<td> no

<input label="no" type="radio" name="authorized[]'.$c.'" id="no'.$c.'"value="no" >

<input name="auth[]" type="hidden" class="mailclass" id="auth'.$c.'" value="" />

</td>

This code will pass the no value, but if someone were to reverse that decision and re-select send, it does not switch the value back to 1.

$(document).ready(function() {

var x='1'

$( "input:radio" ).each(function() {

$(this).click(function() {

x=$(this).val();

alert(x);

$(this).next('.mailclass').val(x);

})

})

})

网友答案:

It's your selector. The $(this).next... isn't working. Using click is more better for your coding than each I did this:

var x='1';
$( "input:radio" ).click(function() {        
        x=$(this).val();
        $('input.mailclass').val(x);
    })
});

And, here's a working jsfiddle.

Hope this helps! (Disclaimer: I can only see a small part of your code, but the root cause is your selector. Feel free to use a better selector if mine isn't optimal. :-)

分享给朋友:
您可能感兴趣的文章:
随机阅读: