用于启用表单字段的单选按钮

时间:2011-10-02 23:03:36

标签: javascript jquery

我有一个包含两个单选按钮的表单,我想在选中单选按钮时启用相应的字段。我的代码不起作用,这里是:

<head>
<script type="text/javascript" src="jquery.js"></script>          
<script type="text/javascript">
$(document).ready(function() {

    var $type = $('select'),
        $field1 = $("#fieldID1"),
        $field2 = $("#fieldID2");

    $type.change(function() {

        var isDisabled = ($type.filter(":checked").val() === "1");

        $field1.prop("disabled", !isDisabled);
        $field2.prop("disabled", isDisabled);

    }).change();
});
</script>
</head>

我的HTML主体就像:

<body>

<form action="" method="post">

<input type="radio" name="select" value="1" />
<input type="text" name="fieldID1" id="fieldID1"/>

<input type="radio" name="select" value="2" />
<table id="fieldID2">
    <tr>...</tr>
    <tr>...</tr>
    ...
</table>

<input type="submit" value="Add">
</form>
</body>

我引用了this post中的代码。谁能指出我的问题是什么,我对jQuery不太熟悉,谢谢!

仍然无法修复......

2 个答案:

答案 0 :(得分:1)

    $fieldID1.prop("disabled", !isDisabled);
    $fieldID2.prop("disabled", isDisabled);

应该是:

    $field1.prop("disabled", !isDisabled);
    $field2.prop("disabled", isDisabled);

答案 1 :(得分:0)

$(document).ready(function() {

    var $type = $(':radio[name="select"]'),
        $field1 = $("#fieldID1"),
        $field2 = $("#fieldID2");

    $type.change(function() {

        var isDisabled = ($type.filter(":checked").val() === "1");

        $field1.prop("disabled", !isDisabled);
        $field2.prop("disabled", isDisabled);

    }).change();
});