2011-10-02 130 views
1

我有一个包含两个单选按钮的表单,我希望在单选按钮被选中时启用相应的域。我的代码只是不工作,那就是:单选按钮启用表单域

<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不太了解,谢谢!

仍无法修复......

+0

你必须通过名称来选择:'输入[名称= “选择”]'。 –

+0

它与刚才使用$('select')相同,我试过了。 – Michael

回答

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

也许应该是:

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

对不起,我的坏,它的错字。修复。 – Michael

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(); 
}); 
+0

$ type的定义变化没有任何区别〜 – Michael

+0

您是否收到任何错误?检查http://jsfiddle.net/Fnhan/1/ –

+0

没有错误返回在我的网页上,它似乎JavaScript只是没有工作... – Michael