简体   繁体   中英

How to get the last selected radio button id in jQuery?

I want to get the id of the last selected radio button value for passing to the database. The radio buttons are working properly. But unable to get the id of the last selected radio button.

Here is my HTML code.

$jsqla = mysql_query("select * from products where id='$product_id'") or die(mysql_error());
$jfeta = mysql_fetch_assoc($jsqla);

$formats = explode(";", $jfeta['formats']);

<div class="">
    <?php foreach($formats as $v){ ?>
        <label style="line-height: 1.25em;display: block;width: 100px;margin-right: 10px;float: left;">                         
            <div id="format-id_<?php echo $v?>" style="border: 1px solid;border-radius: 9px;text-align: center;padding-top: 10px;padding-bottom:10px;padding-left: 3px;padding-right: 3px;border-color: #cccccc;font-family: 'SSemibold'; font-size: 13px; color: #44b7da;">                                
                <input type="radio" value="<?php echo $v; ?>" name="abc" style="visibility:hidden;" id="<?php echo $v ?>" onClick="changeColour(this)"/>
                <span style="margin:-17px auto auto 0px;display:block;"><?php echo $v; ?></span>                            
            </div>                      
        </label>
    <?php } ?>      
</div>

Here is my jQuery code.

$(document).ready(function(e) {
    var $radios = $(':radio'); 
    $divs = $radios.parent(); 
    var cvbl = "";

    $radios.bind('change', function() {
      var $this = $(this),
      $div = $this.parent();
      $div.css('background-color', this.checked ? '#cccccc' : '#ffffff');
      $divs.not($div).css('background-color', '#ffffff');

      var value = $(this).val();
      cvbl = value;
      alert(cvbl);
      alert($('.someClass:last') );
    });

 });

Replace your script with the below one

$(document).ready(function(e) {
    var $radios = $(':radio');
    $divs = $radios.parent();
    var cvbl = "",
        radioId = "";

    $radios.bind('change', function() {
        var $this = $(this),
            $div = $this.parent();
        $div.css('background-color', this.checked ? '#cccccc' : '#ffffff');
        $divs.not($div).css('background-color', '#ffffff');

        var value = $(this).val();
        cvbl = value;
        radioId = $(this).attr('id');
        alert(radioId);
        alert(cvbl);
        alert($('.someClass:last'));
    });

});

Use the variable radioId wherever you want it will always give you the id of the last selected radio button.

You can try below code :

if ($('input[name$="your radio button name"]:last').is(':checked')) {
    alert('the last radio');
    return false;

}

If you don't have radio buttons name then try below

 if ($('input[type="radio"]:last').is(':checked')) {
    alert('the last radio');
    return false;

}

This Worked for me...

$(".clsRadio").on("mousedown", function () {                  
var LastSelectedRadioButton = $('input[type=radio]:checked').attr('id');    
});

clsRadio is a class of my radio button group.

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