[jQuery]兄弟siblings()的範例

  • 1703
  • 0

摘要:[jQuery]兄弟siblings()的範例

畫面上多個radiobutton,最後一個radiobutton的隔壁有一個textbox,如果使用者在最後一個textbox要打字的話

就強制選擇最後一個radiobutton

下面是html

<table>
    <tr>
        <td>
            <input type="radio" name="rblTest" value="一號" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="radio" name="rblTest" value="二號" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="radio" name="rblTest" value="三號" />
            <asp:TextBox ID="txtOthers" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>

下面是jQuery語法

$(document).ready(function () {
    var txtOthers = $("input[name*='txtOthers']");
    txtOthers.click(function () {
        //點選textbox的時候,就連帶選擇隔壁的radio button
        $(this).siblings("input[type='radio']").prop("checked", true);
        // $("").prop("checked", true);
    });
});