[英]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.