简体   繁体   中英

Alert if a radio button is selected

I don't know javascript, so this may be on the wrong track anyway. I have shipping methods that can be selected via radio button. I'm trying to create an alert if a certain shipping method is selected, however the radio value changes based on the available shipping options.

<label id="shippingMethod"><input type="radio" name="selectedShippingMethod" id="shippingCheck" value="5"> <span class="ShipperName">Collect Shipping</span>  <em class="ShipperPrice ProductPrice">$0.00</em></label>

and here is the alert code I'm trying to use

$(document).ready(function(){
    $('input:radio[name="selectedShippingMethod"]').change(function() {
        if ($(this).val() == '5'){
            alert('ALERT TEXT TO POPUP');
        }
    });
});

Is there a way I can make it trigger an alert based on ShipperName "Collect Shipping" instead of the radio value, or is there a better way to be doing this to begin with.

$(document).ready(function () {

    var $radios = $('input:radio[name="selectedShippingMethod"]');

    $radios.change(function () {
        var $selected = $radios.filter(':checked');
        if ( $selected.val() == 5 ) {
            alert( $selected.parent().text() );
        }
    });
});

Joseph Silber, the only change I would suggest is setting the alert to "Collect Shipping" per the OPs original goal.

Your function helped me with a similar solution I was looking for (Thanks!)

$(document).ready(function () {
  var $radios = $('input:radio[name="selectedShippingMethod"]');
  $radios.change(function () {
    var $selected = $radios.filter(':checked');
    if ( $selected.val() == 5 ) {
        alert("Collect Shipping" );
        }
    });
});

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