如果复选框被选中,则更改文本框文本 [英] change text box text if checkbox is checked

查看:102
本文介绍了如果复选框被选中,则更改文本框文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据是否勾选复选框来更改文本框文本。因此,如果用户选中该复选框,则文本框会显示一些文本,如果用户未选中该复选框,则会显示一些不同的文本。

I want to change text box text depending if checkbox is checked or not. So if the user checks the checkbox, the text box shows some text and if the user unchecked the checkbox, it shows some different text.

HTML:

<input id="check" type="checkbox" />
<input id="txt" type="text" value="aaaa" />

jQuery:

jQuery:

$('input[type=checkbox]').each(function () {
    if (!$('input[type=checkbox]').is(':checked')) {
        $('#txt').val('checked');
    }
    else{
        $('#txt').val('unchecked');
    }
});

JSFiddle Demo

JSFiddle Demo

推荐答案

尝试绑定点击事件:

$('input[type=checkbox]').click(function () {
    if ($('input[type=checkbox]').is(':checked')) {
        $('#txt').val('checked');
    }
    else{
        $('#txt').val('unchecked');
    }
});

这篇关于如果复选框被选中,则更改文本框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆