2012-03-30 117 views
1

我有以下的jquery,什么是让用户填写表格中的其他字段,如果复选框被选中。我想要的是清除额外的输入字段值,如果复选框未选中。jQuery的清除输入字段,如果复选框未选中

$(document).ready(function() { 
    $('input:checkbox').attr('checked', false); 
    $('#xml').hide(); 
    $('#checkbox').click(function() { 
     $('#xml')[this.checked ? "show" : "hide"](); 
    }); 
    $('input[type="checkbox"]').click(function() { 
     if (this.checked) { 
      $('div#xml p label input').attr('required', true); 
     } 

     else 

      $('div#xml p label input').removeAttr('required', true); 

     //Here should the corresponding code to be pasted 
        //$('div#xml p label input').val = (''); 

    }); 

$(function() { 
    $("#datepicker").datepicker(); 
}); 
}); 

回答

2

解决的办法是:

$('div#xml p label input').val(""); 
+0

非常感谢。有时我会在不受欢迎的语法 – lgt 2012-03-30 08:20:38

+0

之间松懈,它偶尔会发生在我们所有人身上:) – 2012-03-30 08:21:15

0

你应该做

$('input[type="checkbox"]').click(function() { 
    if (this.checked) { 
     $('div#xml p label input').attr('required', true); 
    }else{ 
     $('div#xml p label input').removeAttr('required', true); 
     $('div#xml p label input').val(''); 
    } 

}); 
相关问题