用jquery选择无线电

时间:2015-01-18 00:57:26

标签: jquery select

当我点击按钮时,我需要选择无线电的值 这是我的代码。 jquery代码必须以此代码开头:

$('.button').click(function(){});

我写了这段代码,但它不起作用:

$('.button').click(function(){
    var t = $(this).parent().find('td div#select_type').children('input[checked=checked]').html(); });

 <table>
        <tbody>
            <tr class="row">
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td width="130">
                    <div id="select_type">
                        <input value="0" id="select_type_0" type="radio" name="select_type" />
                        <label for="select_type_0"></label>
                        <input value="1" id="select_type_1" type="radio" name="select_type" />
                        <label for="select_type_1"></label>
                        <input value="2" id="select_type_2" type="radio" name="select_type" checked=checked />
                        <label for="select_type_2"></label>
                    </div>
                </td>
                <td class="button button_bt" style=" border: none">
                    <a>button</a>
                </td>
            </tr>
            <tr>
            ...
            </tr>
        </tbody>
    </table>

3 个答案:

答案 0 :(得分:1)

以下是解决方案

var t = $(this).parent().find('td div#select_type').children('input:checked').val();alert(t);

<强>演示 JSFiddle

答案 1 :(得分:0)

我无法找到你的错误,但我可以提供其他解决方案

首先让我们为元素提供id

<table>
    <tbody>
        <tr class="row">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td id="form" width="130">
                <div id="select_type">
                    <input value="0" id="select_type_0" type="radio" name="select_type" />
                    <label for="select_type_0"></label>
                    <input value="1" id="select_type_1" type="radio" name="select_type" />
                    <label for="select_type_1"></label>
                    <input value="2" id="select_type_2" type="radio" name="select_type" checked=checked />
                    <label for="select_type_2"></label>
                </div>
            </td>
            <td id="btn" class="button button_bt" style=" border: none">
                <a>button</a>
            </td>
        </tr>
        <tr>
        ...
        </tr>
    </tbody>
</table>

比调用这样的jquery

$('.button').click(function(){
var t = $("#form input[type='radio']:checked").val();});

它会正确分叉

答案 2 :(得分:0)

<强> JS

$('.button > a').click(function(){
    var myValue= $('div#select_type input:radio:checked').val();
    alert(myValue);
});

也可以在fiddle

中找到