jquery中的单选按钮控件

Radio Button Control in jquery

本文关键字:控件 单选按钮 jquery      更新时间:2023-09-26

我是Web开发和jQuery的新手
我正在尝试构建一个带有两个RadioButton控件的ASPX页面,这些控件必须执行以下操作:

在页面加载时,必须根据ASPX页面上对象的标志选择其中一个。让我们称之为客户。Id。如果Id为true,则必须设置一个选择RadioButton,否则必须设置RadioButton 2。

在页面加载后的任何时候,用户选择一个RadioButton,另一个必须取消选择。

当点击RadioButton二时,隐藏一个名为"员工表"的Table,当点击RadioButton一时,显示Table

有人能告诉我如何在jQuery函数中获得此功能吗?

不确定.NET,但在经典ASP中,您会编写这样的变量<%=customerID%>。

在jQuery中,我认为你可以这样做:

<input type="radio" id="radio1"> Yes
<input type="radio" id="radio2"> No
<table border="1" id="employeeTable">
    <tr><td>This is the table</td></tr>
</table>

然后是一些jQuery:

$(document).ready(function() {
    var customerID = <%=customerID%> // asp variable
    if (customerID != "") {
        $('#radio1').prop('checked', 'checked');
    } else {
        $('#radio2').prop('checked', 'checked');
    }
    $('#radio1').click(function() {
        $('#employeeTable').fadeIn('fast');
    })
    $('#radio2').click(function() {
        $('#employeeTable').fadeOut('fast');
    })
})

你可以在这里看一看/玩一玩:http://jsfiddle.net/qcLtX/7/

尝试将customerID值更改为空,如var customerID = ""

祝好运

更新

我使用.prop的地方:如果您使用的是jQuery 1.6或更高版本,则应该使用.prop,否则,则使用.attr

单选按钮按其名称属性分组,如so(source)。

<form>
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form>

如果单选按钮已分组,则选择其中任何一个按钮将自动delselect该组中的所有其他按钮。

因此,按钮不能有一个不同的名称。如果你想区分单选按钮(不参考它们的值),你应该添加一个id。

<input type="radio" name="sex" id="m" value="male" />

您可以在标记中以声明方式或使用jquery在页面加载上设置选定的单选按钮。

声明版本:

<input type="radio" checked="checked" name="sex" value="male" />

jQuery:

$(document).ready(function(){
    $("#m").attr("checked", "checked");
});