2012-11-26 125 views
0

例如说,我有以下几点:勾选复选框被点击?

<input type="checkbox" id="chk-box1" name="chk-box1" value="Check 1">Check 1 

<input type="text" id="textbox1" name="textbox1" value=""> 

我可以设置文本框的值时,通过做点击“CHK-BOX1”:

$('#chk-box1').change(function(){ 
    var chk = $(this); 
    $('#textbox1').val("Textbox 1 is checked")('selected', chk.attr('checked')); 
}) 

,但我怎么可能设置的值当该框未选中时,将文本框的内容清空? $('#textbox1').val("")

回答

3

试试这个,

Live Demo

如果你想显示的消息,而不是真或假

$('#chk-box1').change(function(){  
    if(this.checked) 
     $('#textbox1').val("Textbox 1 is checked"); 
    else 
     $('#textbox1').val(""); 
}) 
4

尝试使用this.checked财产

$('#chk-box1').change(function(){ 
    if (this.checked) { 
     $('#textbox1').val("Textbox 1 is checked"); //('selected', chk.attr('checked')); 
    } else { 
     $('#textbox1').val(""); 
    } 
}) 

或简单地说,

$('#chk-box1').change(function(){ 
    $('#textbox1').val(this.checked?"Textbox 1 is checked":""); //('selected',  
});