简体   繁体   English

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

[英]if Checkbox is Checked, then Add Value to Input Field

i'm trying to change the value of an text input to no if checked or yes if not checked, this with a checkbox but i cant seem to get it working.. 我正在尝试将文本输入的值更改为“否”(如果选中)或“是”(如果未选中),带有复选框,但是我似乎无法使其正常工作。

<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");
});
});

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

is there something i miss here? 我在这里想念什么吗? please advise. 请指教。

You just need to include jQuery in your fiddle. 您只需要在小提琴中包含jQuery。 Theres a dropdown on the left for libraries. 左侧有一个下拉菜单。

This should work 这应该工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM