简体   繁体   English

如何根据选择的单选按钮填充文本框?

[英]How to populate textbox depending on which radio button is selected?

I am working with html and jquery. 我正在使用html和jquery。 I have a form in which I have few radio buttons and few text box. 我有一个表单,其中有几个单选按钮和几个文本框。 I need to populate my textbox depending on which radio button I am selecting. 我需要根据选择的单选按钮填充文本框。

Here is my jsfiddle 这是我的jsfiddle

Now what I need to do is - After selecting Process button If I am selecting UpdateTest radio button, then I need to add hello in the AddNodeText box but if I am selecting DataTest radio button, then I need to add world in the AddNodeText box and apart from that, I don't need to do add anything in AddNodeText box. 现在,我需要做的是-选择“ Process按钮后如果我选择“ UpdateTest单选按钮,则需要在AddNodeText框中添加hello ,但是如果我选择“ DataTest单选按钮,则需要在AddNodeText框中添加world ,并且除此之外,我不需要在AddNodeText框中添加任何内容。

Is this possible to do using jquery? 使用jquery可以做到吗?

Have you tried this? 你有尝试过吗?

$('input[name="client"]').click(function() {
    if ($(this).attr('id') === 'updatetest')
    {
        $('#conf').val('hello');
    }
    else if ($(this).attr('id') === 'datatest')
    {
        $('#conf').val('world');
    }
    else
    {
        $('#conf').val('');
    }
});

DEMO 演示

This is nicely done using data attributes. 使用数据属性可以很好地做到这一点。

Set a data attribute 设置数据属性

<input type="radio" data-text="hello"></input>

Add data attribute on click. 点击添加数据属性。

$(".boxes").on('click', function() {
 var text = this.getAttribute('data-text');
 $("input#conf").val(text);
});

Here is your Fiddle, I've updated it and its working. 这是您的小提琴,我已经对其进行了更新。 http://jsfiddle.net/4Nmqk/123/ http://jsfiddle.net/4Nmqk/123/

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

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