简体   繁体   中英

How to get radio button value dynamically in JavaScript

I have six radio button whose id is coming dynamically in foreach loop now i have to get that value outside foreach loop. If any body know solution than please help. Below is my code

<div class="person_option-block">
    <h5>No of Person</h5>
    <?php
        $no_of_persons = $this->Mdl_home->no_of_person_all();
        foreach($no_of_persons as $no_of_person){ ?>
    <label class="person_option">
        <?php echo $no_of_person['person']; ?> 
        ($ <?php echo $no_of_person['price']; ?>/person)
        <input type="radio" class="person-radio" name="no_person" id="<?php echo $no_of_person['id']; ?>" value="<?php echo $no_of_person['id']; ?>"
            onchange="get_makeup_service(<?php echo $no_of_person['person']; ?>)">

        <input type="hidden" id="person_<?php echo $no_of_person['person']; ?>" value="<?php echo $no_of_person['person']; ?> ($<?php echo $no_of_person['price']; ?>/person)">
    </label>
    <?php } ?>
</div>
<div class="pull-right">
    <a href="javascript:void(0);" class="custom-btn" id="step1-next-btn">
        Next
        <i class="fa fa-angle-double-right" aria-hidden="true"></i>
    </a>
</div>

When i click on next button the id of particular radio button should alert

$('#step1-next-btn').click(function(){ 
    $('#step1-block').hide();
    $('#step2-block').show();
}); 

Use $('input[name=no_person]:checked').val(); expression to get the selected radio button value.

Try Using,

$(function(){

if($("#radioID").prop("checked")) {

   var radioValue = $("#radioID").val();

}

});

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