简体   繁体   English

在选择一个选项之前,如何禁用下一个按钮?

[英]How do I get the next button to be disabled until an option is selected?

I have the following code.我有以下代码。 The next button is currently disabled, however, I want the next button to not be disabled when an option from my select input is chosen (not including the first option which is disabled).下一个按钮当前被禁用,但是,我希望在选择我的 select 输入中的选项时不禁用下一个按钮(不包括第一个被禁用的选项)。

  $(document).ready( function(){
var $option = $("option:contains('Please select an option')");
var $btnNext2 = $('button.cf7mls_next');
$option.attr('disabled',true);
$btnNext2.attr('disabled',true);
});

I am assuming there will need to be an if statement involved somewhere, however, I am working with contact form 7 on WordPress so it may be slightly different.我假设某处需要涉及 if 语句,但是,我正在使用 WordPress 上的联系表 7,因此它可能略有不同。

Any help would be appreciated, thank you.任何帮助将不胜感激,谢谢。

To do what you require you need to attach a change event handler to the select which detects what the selected value is, and then enables/disables the button based on that value:要执行您需要的操作,您需要将change事件处理程序附加到select以检测所选value是什么,然后根据该值启用/禁用按钮:

 jQuery($ => { let $btn = $('.cf7mls_next'); $('select').on('change', e => { $btn.prop('disabled', e.target.value == ''); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <select> <option value="">Please select an option</option> <option value="Foo">Foo</option> <option value="Bar">Bar</option> <option value="Fizz">Fizz</option> <option value="Buzz">Buzz</option> </select> <button class="cf7mls_next" disabled>Next</button>

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

相关问题 如何获取上一个/下一个未禁用选项的索引(相对于所选选项)? - How to get the index of the previous/next non-disabled option (relative to the selected option)? 如何在选择 2 个产品进行比较之前保持按钮禁用 - How to keep button disabled until 2 products are selected for comparison WooCommerce-在选择以上一项之前,如何禁用属性选项菜单? - WooCommerce - how do I disable attribute option menus until the one above is selected? 如何获得所选选项的文本值? - How do I get the text value of a selected option? 我如何获得“选择的数据”标签 - How do i get Option Selected Data tag 保持输入禁用,直到在html中选择了正确的选项 - Keep input disabled until correct option is selected in html 如何在选择选项时使用下一个按钮进行验证用户输入? - How to validate user input on dropdown with next button only if option selected? 我需要将下一个按钮设置为禁用,直到选中任何单选按钮 - I need to set next button as disabled till any radio button got selected 获取隐藏下拉列表禁用选定选项的值 - get value of hidden dropdown disabled selected option jQuery - 如何在选定的下拉列表中获取下一个选项的值? - jQuery - how can I get a value of the next option in a dropdown after the selected one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM