简体   繁体   English

在复选框上更改文本输入值

[英]Change Text input value on checkbox select

I am trying to input the value of a checkbox into a text input. 我正在尝试将复选框的value输入到文本输入中。

Let's say that the input box is empty - you click on the checkbox, and the value assigned to the checbkox is being shown inside the input box. 假设输入框为空-单击复选框,分配给checbkox的值将显示在输入框内。

$('input.lowercase').on('change', function(){
      if ( $(this).is(':checked') ) {
$("input.qwer").on("keyup",function () {
    $("input.qwer").html($(this).val());
}); } } );

No matter what I do I can't get this to work. 无论我做什么,我都无法正常工作。 Any help? 有什么帮助吗? http://jsfiddle.net/6ycnzrty/ http://jsfiddle.net/6ycnzrty/

[EDITED] (As per your needs) [编辑](根据您的需要)

Demo on Fiddle 小提琴演示

HTML: HTML:

<input type="text" class="output" value="" />
<br>
<input type="checkbox" class="qwer" value="qwerty">Input value of this checkbox(qwert)
<br>
<input type="checkbox" class="numbers" value="1234567890">Input value of this checkbox(numbers)
<br>
<input type="checkbox" class="asdfg" value="asdfg">Input value of this checkbox(asdfg)
<br>
<input type="checkbox" class="zxcvb" value="zxcvb">Input value of this checkbox(zxcvb)

JavaScript: JavaScript:

$('input.qwer').on('change', function () {
    if ($(this).is(':checked')) {
        $('.output').val($('.output').val() + $(this).val());
    } else {
        $('.output').val($('.output').val().replace($('.qwer').val(), ''));
    }
});

$('input.numbers').on('change', function () {
    if ($(this).is(':checked')) {
        $('.output').val($('.output').val() + $(this).val());
    } else {
        $('.output').val($('.output').val().replace($('.numbers').val(), ''));
    }
});

$('input.asdfg').on('change', function () {
    if ($(this).is(':checked')) {
        $('.output').val($('.output').val() + $(this).val());
    } else {
        $('.output').val($('.output').val().replace($('.asdfg').val(), ''));
    }
});

$('input.zxcvb').on('change', function () {
    if ($(this).is(':checked')) {
        $('.output').val($('.output').val() + $(this).val());
    } else {
        $('.output').val($('.output').val().replace($('.zxcvb').val(), ''));
    }
});

Try This :- 尝试这个 :-

$('input.qwer').on('change', function(){
   if ( $(this).is(':checked') ) {
     $("input.output").val($(this).val());
   } 
   else{ $("input.output").val("123"); }
});

With above code if checkbox is unchecked then textbox having class 'output' will get its initial view ie '123',if you don't need this functionality then try this : 使用上面的代码,如果未选中复选框,则具有“输出”类的文本框将获得其初始视图,即“ 123”,如果不需要此功能,请尝试以下操作:

$('input.qwer').on('change', function(){
   if ( $(this).is(':checked') ) {
     $("input.output").val($(this).val());
   } 
});

EDIT :- 编辑:-

DEMO 演示

Try 尝试

var $chk = $('input.qwer').on('change', function () {
    //if the checkbox is checked and output is empty set the value
    if (this.checked && !$output.val()) {
        $output.val(this.value)
    }
});
var $output = $("input.output").on("change", function () {
    //when the value of output is changed as empty and checkbox is checked then set the value to checkbox
    if (!this.value && $chk.is(':checked')) {
        this.value = $chk.val();
    }
});

Demo: Fiddle 演示: 小提琴

$("input.qwer").on("change",function () {
    if($(this).is(":checked"))
    $("input.output").val($(this).val());
    else
        $("input.output").val("123");
});

DEMO 演示

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

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