简体   繁体   中英

how to save radio button clicked value in hidden field?

<script>
    $(document).ready(function(){
        var value = $('input[name="yesno"]').change(function(){
            if($('#imageCheck').prop('checked')){
                // alert('Image Option checked!');
            }else if($('#textCheck').prop('checked')){
                // alert('Text Option Checked!');
            }
        });
    });
</script>

<input type="radio" onclick="javascript:imageTextCheck();" name="yesno" 
  id="imageCheck"/>&nbsp&nbsp&nbsp<b>Text</b>
<input type="radio" onclick="javascript:imageTextCheck();"name="yesno" 
  id="textCheck"/>

<input type="hidden" name="radioCheck" id="radioCheck" value=""/>

how i can save the value of radio button in hidden field. If i Click on image it saves image value.

 $(document).ready(function(){ var value = $('input[name="yesno"]').click(function(){ $('#radioCheck').val($(this).val()); }); setInterval(() => console.log( $('#radioCheck').val() ), 1000); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="radio" name="yesno" value="imageCheck" id="imageCheck"/><label for="imageCheck">imageCheck</label> <br /> <input type="radio" name="yesno"value="textCheck" id="textCheck"/><label for="textCheck">textCheck</label> <input type="hidden" name="radioCheck" id="radioCheck" value=""/> 

hope this one helps you :)

the setInterval is just to print out the hidden field's value each and every second so when you click on a radio button you can see that it sets the hidden input's value

Here is a simple solution for your problem: I have written one sample of code to explain the problem:

First : OnClick of radio button save the value of radio button if it is checked.

var radioValue = $("input[name='yesno']:checked").val();

Second : Assign the checked radio button value to the hidden input field.

 $(document).ready(function(e) { $("input[type='radio']").click(function(){ var radioValue = $("input[name='yesno']:checked").val(); if(radioValue) { $("#radioCheck").val(radioValue); console.clear(); console.log($("#radioCheck").val()); } }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="image_radioButton"> <input id="image_radio_check" value="imageButtonChecked" type="radio" class="radio_button" name="yesno" /> Image Radio Button </div> <div class="text_radioButton"> <input id="text_radio_check" value="textButtonChecked" type="radio" class="radio_button" name="yesno" />Text Radio Button </div> <div class="hidden_input"> <input type="hidden" name="radioCheck" id="radioCheck" value=""/> </div> 

Hope this may hep you in solving your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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