0

我想在文本框 B 的表单提交期间验证输入,如果它由无线电 2 启用。文本框 B 的输入验证不适用于以下代码:

<script type="text/javascript">
$(document).ready(function() 
{
    $("#form").validate(
    {
        rules: {B: "required",}
    }); 

   ​$(':radio').change(function() 
   {
        $('#B').prop('disabled', !(this.value == '2' && this.checked));
   })​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​;         
});
</script>

<form id="form" name="form" method="post" action="">
<table><tr>
<td width><input type="radio" name="A" id="1" value="1" />1</td>
<td width><input type="radio" name="A" id="2" value="2" />2</td>
</tr></table>
<input name="B" type="text" id="B" disabled="disabled"/>
<input type="submit" name="form" id="Submit" value="Submit" />
</form>
4

1 回答 1

1

尝试这个,

$(document).ready(function() 
{
    $("#form").validate({
            rules: {
                B: "required",
            }
        });
    $('input:radio[name="A"]').change(
    function(){
        if ($(this).is(':checked') && $(this).val() == '2') {
            $('#B').removeAttr('disabled');
        }else if($(this).is(':checked') && $(this).val() == '1'){
            $('#B').attr('disabled', 'disabled');
        }
    });
});
于 2012-12-12T03:35:50.313 回答