简体   繁体   中英

Disable select input if option value exists

I want to disable the select box if Free Shipping is available.

<select name="shipping_method" id="shipping_method">
    <option value="">Select shipping</option>
    <option value="free_shipping">Free Shipping</option>
    <option value="international_delivery">International: $30.00</option>
</select>

I've tried this (without knowing if it would work):

$(document).ready(function(){
    if($("option").val() == "free_shipping") {
        $("select").prop("disabled");
    }
});

And then assuming on form submit:

$(document).ready(function(){
    $("form").submit(function() {
        $("select", this).prop("disabled", false);
    });
});

select the select

$('#shipping_method').change(function(){
    var j = $(this);
    if(j.val() == 'free_shipping') {
        j.attr('disabled', true);
    }    
    else {
        j.removeAttr('disabled');
    }
}).trigger('change');

You can use $.has

$('#shipping_method').has('option[value="free_shipping"]').prop("disabled", true);

Demo: Fiddle

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