populate text box on click of radio button

后端 未结 4 775
醉酒成梦
醉酒成梦 2021-01-26 18:10

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 \'Yes\' radio button, I want the text \

相关标签:
4条回答
  • 2021-01-26 18:39

    do you want this?

    http://jsfiddle.net/viralshah/KNct8/3/

    Hope this resolve your problem.

    0 讨论(0)
  • 2021-01-26 18:40

    This might help you

    <form action="test1.asp" id="testform">
    <input type="text" id="test3" value="">
    <input type="radio" id="submit" onclick="modify_value()"/>
    </form>
    <script type="text/javascript">
    function modify_value()
    {
    var hidden_field = document.getElementById('test3');
    hidden_field.value = 'testvalue';
    
    }
    </script>
    
    0 讨论(0)
  • 2021-01-26 18:45

    Try this. Should be cross-browser, but it's worth testing in a few older browsers.

    <html>
    <body>
    <p><span>User clicked: </span><input type="text" id="userAnswer"/></p>
    <input type="radio" name="userAnswerChoice" value="Yes"/>
    <input type="radio" name="userAnswerChoice" value="No"/>
    <input type="radio" name="userAnswerChoice" value="Maybe"/>
    <script type="text/javascript">
    function handleUserAnswer(e){
        var el = e.target || e.srcElement;
        document.getElementById('userAnswer').value = el.value;
    }
    var radios = document.getElementsByTagName('input');
    for(var i = 0; i<radios.length; i++){
        var r = radios[i];
        if(r.getAttribute('name') == 'userAnswerChoice'){
            if(r.addEventListener){
                r.addEventListener('change',handleUserAnswer);
            }else if(r.attachEvent){
                r.attachEvent('onchange',handleUserAnswer);
            }else{
                r.onChange = handleUserAnswer;
            }
        }
    }
    </script>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-26 18:46

    U have an onclick event so add onclick="function" and in that function u set text... U can get text from radio box with some javascript like that How do I get the text of a radio button (not the value)

    0 讨论(0)
提交回复
热议问题