当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Populate textbox based on selected radio button

问题描述:

I have 3 radio buttons, when one of them is selected I want to automatically put text in a textbox.

For example, If the user selected the 'TBA' radio button, I want the text 'To Be Advised' put in the textbox, and if they select another radio button I want it to be cleared.

Can anyone help me??

http://jsfiddle.net/ej2hf/1/

Thanks in advance!

网友答案:

Try this then

http://jsfiddle.net/ipsjolly/KNct8/

$('input[name="dest_type"]').on('change', function() {
    if($(this).val() == "tba"){
        $('input[type="text"]').val($(this).val());
    }
    else
    {
        $('input[type="text"]').val('');
    }

});

OR

http://jsfiddle.net/ipsjolly/KNct8/1/

$('input[name="dest_type"]').on('change', function() {
     if($(this).val() == "tba"){
    $('input[type="text"]').val('To Be Advised');
    }
    else
    {
      $('input[type="text"]').val('');
    }

});
网友答案:

This should do:

$('input[name="dest_type"]').on('change', function() {
    $('input[type="text"]').val($(this).val());
});

Live demo: http://jsfiddle.net/ej2hf/2/

网友答案:

In addition to populate the textbox with the value which is selected by default on page load use the below code:

$(document).ready(function(){
$('input[type="text"]').val($('input[name="dest_type"]').val());
$('input[name="dest_type"]').on('change', function() {     
$('input[type="text"]').val ($(this).val()); }); 
});
网友答案:

Assuming that you are or can use the latest jQuery, you can use this:

$(function() {
    $('input[name="dest_type"]').on("change", function() {
        $('input[type="text"]').val($(this).val());
    });
});

See the Fiddle!


If what you need if to toggle the value of TBA, use this:

$(function() {
  $('input[name="dest_type"]').on("change", function() {
    $('input[type="text"]').val( (($(this).val()=='tba')?($(this).val()):('')) );
  });
});

See this Fiddle!


If none of this is what you are trying to do, please rephrase the question or add some more information!

网友答案:

try this,

$(document).ready(function () {
$('input:radio[name="dest_type"]').change(function () {
   $("input:text").val($(this).val());
});
})
分享给朋友:
您可能感兴趣的文章:
随机阅读: