简体   繁体   English

根据按钮重置选择选项

[英]Reset select option based on button

I would like to reset my select options if I click other radio button that doesn't trigger the select div. 如果我点击不触发select div的其他单选按钮,我想重置我的选择选项。

Here's my sample: 这是我的样本:

 $(document).ready(function() { $(':radio').on('change', function() { var title = $(this).val(); // Get radio value if ($(this).attr('id') === 'theme2') { $('.occ_select').addClass('l').removeClass('off'); } else { //...otherwise status of radio is off $('.occ_select').removeClass('l').addClass('off'); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <br> <input type="radio" id="theme1" name="cake_theme" value="dedi" /> Dedication <br> <input type="radio" id="theme2" name="cake_theme" value="occ" /> Others <div id="occassion" class="occ_select off"> <select name="occassion_select" id="occa_slct"> <option disabled selected value>-- Select one --</option> <option value="otheerrr"> otheerrr </option> <option value="other1"> other1 </option> <option value="other2"> other2 </option> </select> </div> 

So what I would like to happen, that everytime I choose "dedication button" , the select option will reset to <option disabled selected value>-- Select one --</option> . 所以我想要发生的是,每当我选择“奉献按钮”时,选择选项将重置为<option disabled selected value>-- Select one --</option> I don't know what am I missing in my jQuery to trigger that reset event. 我不知道我的jQuery中缺少什么来触发重置事件。

Thank you so much in advance! 非常感谢你提前!

 $(document).ready(function() { $(':radio').on('change', function() { var title = $(this).val(); // Get radio value if ($(this).attr('id') === 'theme2') { $('.occ_select').addClass('l').removeClass('off'); } else { //...otherwise status of radio is off $('.occ_select').removeClass('l').addClass('off'); } $("#occa_slct option").eq(0).prop("selected", title == "dedi") //set to first option when value of title is dedi $("#occa_slct").prop("disabled", title == "dedi") //disable select when value of title is dedi }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <br> <input type="radio" id="theme1" name="cake_theme" value="dedi" /> Dedication <br> <input type="radio" id="theme2" name="cake_theme" value="occ" /> Others <div id="occassion" class="occ_select off"> <select name="occassion_select" id="occa_slct"> <option disabled selected value>-- Select one --</option> <option value="otheerrr"> otheerrr </option> <option value="other1"> other1 </option> <option value="other2"> other2 </option> </select> </div> 

To achieve this you can use val('') to reset the value of the select to it's default. 要实现此目的,您可以使用val('')将select的值重置为默认值。 I'd also suggest that you disable the select unless the appropriate radio input is selected so that the user knows an option is required. 我还建议您禁用select除非select了适当的无线电输入,以便用户知道需要一个选项。 Try this: 尝试这个:

 $(document).ready(function() { $(':radio').on('change', function() { var title = $(this).val(); if (this.id === 'theme2') { $('.occ_select').addClass('l').removeClass('off'); $('#occa_slct').prop('disabled', false); } else { $('.occ_select').removeClass('l').addClass('off'); $('#occa_slct').prop('disabled', true).val(''); // reset value here } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <br> <input type="radio" id="theme1" name="cake_theme" value="dedi" /> Dedication<br> <input type="radio" id="theme2" name="cake_theme" value="occ" /> Others <div id="occassion" class="occ_select off"> <select name="occassion_select" id="occa_slct" disabled="disabled"> <option disabled selected value>-- Select one --</option> <option value="otheerrr"> otheerrr </option> <option value="other1"> other1 </option> <option value="other2"> other2 </option> </select> </div> 

try this: 尝试这个:

    <br> <input type="radio" id="theme1" name="cake_theme" value="dedi" />  Dedication
    <br> <input type="radio" id="theme2" name="cake_theme" value="occ" /> Others


    <div id="occassion" class="occ_select off">
        <select name="occassion_select" id="occa_slct">
            <option disabled selected value="0">-- Select one --</option>
            <option value="otheerrr"> otheerrr </option>
            <option value="other1"> other1 </option>
            <option value="other2"> other2 </option>
        </select>
    </div>

and JS 和JS

 $(document).ready(function () {
    $(':radio').on('change', function () {
        $("#occa_slct").val(0);
        $("#occa_slct").change();
        var title = $(this).val(); // Get radio value
        if ($(this).attr('id') === 'theme2') {
            $('.occ_select').addClass('l').removeClass('off');
        } else { //...otherwise status of radio is off
            $('.occ_select').removeClass('l').addClass('off');
        }
    });
});

Try this. 尝试这个。 This will work. 这会奏效。

$('#occassion').prop('selectedIndex',0); $( '#occassion')丙( '的selectedIndex',0);

Hope this helps!!! 希望这可以帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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