【问题标题】:Automatically select item in RadioButtonList after clicking TextBox单击 TextBox 后自动选择 RadioButtonList 中的项目
【发布时间】:2017-10-27 15:42:19
【问题描述】:

所以我目前有一个页面,其中包含 RadioButtonList 中的两个单选按钮,后跟两个 TextBox:

<asp:RadioButtonList ID="radioButtonList1" runat="server">
    <asp:ListItem Value="Option1">Option1</asp:ListItem>
    <asp:ListItem Value="Option2">Option2</asp:ListItem>
</asp:RadioButtonList>

<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>

有没有一种简单的方法可以在单击第一个 TextBox 后选择 RadioButtonList 的第一个列表项?这会更加用户友好,因为在继续之前忘记检查值是很常见的。

提前致谢。

【问题讨论】:

    标签: asp.net radiobuttonlist


    【解决方案1】:

    你可以用 jQuery 做到这一点。

    您的 HTML 看起来像这样:

    <input id="ctl00_ContentPlaceHolder1_radioButtonList1_0" type="radio" name="ctl00$ContentPlaceHolder1$radioButtonList1" value="Option1">
    <label for="ctl00_ContentPlaceHolder1_radioButtonList1_0">Option1</label>
    <input id="ctl00_ContentPlaceHolder1_radioButtonList1_1" type="radio" name="ctl00$ContentPlaceHolder1$radioButtonList1" value="Option2">
    <label for="ctl00_ContentPlaceHolder1_radioButtonList1_1">Option2</label>
    
    <input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" id="ctl00_ContentPlaceHolder1_TextBox1">
    <input name="ctl00$ContentPlaceHolder1$TextBox2" type="text" id="ctl00_ContentPlaceHolder1_TextBox2">
    

    jQuery:

    $('#ctl00_ContentPlaceHolder1_TextBox1').focus(function() {
        $('#ctl00_ContentPlaceHolder1_radioButtonList1_0').prop("checked", true);
    });
    

    https://jsfiddle.net/1dqzwtnh/


    或者只是将 Selected="True" 添加到您的 aspx 中的第一个选项中。

    【讨论】:

      【解决方案2】:

      经过一天的研究,我自己找到了答案。 在 Page_Load 函数后面的代码中,我放了这行代码:

      TextBox1.Attributes.Add("OnClick", "document.getElementById('" + radioButtonList1.ClientID + "_0').checked=true;");
      

      我很确定有更好的方法,但现在这似乎工作得很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 2014-01-15
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        相关资源
        最近更新 更多