如果选中复选框,则将值添加到输入字段

if Checkbox is Checked, then Add Value to Input Field

本文关键字:添加 输入 字段 复选框 如果      更新时间:2023-09-26

我正试图将文本输入的值更改为no(如果选中)或yes(如果未选中),这带有复选框,但我似乎无法使其工作。。

<input type="checkbox" name="checkbox" id="checkbox" value="" />
<input type="text" name="text" id="text" value="" />

$(function(){
$('#checkbox').change(function() {
    $("#text").val(($(this).is(':checked')) ? "yes" : "no");
});
});

演示:http://jsfiddle.net/YJRQp/

这里有我想念的东西吗?请告知。

您只需要在fiddle中包含jQuery。左边有一个图书馆的下拉列表。

这应该能在中工作

$(document).ready(function(){
    $("#checkbox").click(function(){
        if ($(this).is(':checked'))
        {
            $("#text").val("Yes");
        }
        else
        {
            $("#text").val("No");
        }
    });
});