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("")

4

2 に答える 2

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',     
});
于 2012-11-26T15:21:47.250 に答える
3

これを試して、

ライブデモ

trueまたはfalseの代わりにメッセージを表示したい場合。

$('#chk-box1').change(function(){      
    if(this.checked)
         $('#textbox1').val("Textbox 1 is checked");
    else
         $('#textbox1').val("");
})
于 2012-11-26T15:21:07.550 に答える