2012-11-26 38 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',  
});