Javascript 禁用<;如果输入字段为空,则为类按钮 javascript jquery input Javascript 禁用<;如果输入字段为空,则为类按钮,javascript,jquery,input,Javascript,Jquery,Input,我知道使用或 我知道使用或 <input id="one" type="text"> <input id="two" type="text"> <a href="#" class="button">OK</a> $(文档).ready(函数(){ //$(“.button”).attr('disabled','disabled”); $(“.button”)。单击(函数(){ 一=$(“#一”).val(); 二=$(“#二”).val(); 如果(一和二){ ///两个字段都已填充。 返回true; } //其中一个或两个为空 返回false; }); });代码> 将事件绑定到两个输入,并检查两个输入是否都有值。然后启用链接 $('#one, #two').blur(function() { if($('#one').val() !== "" && $('#two').val() !== "") { $('.button').attr('href','#'); } else { $('.button').removeAttr('href'); } }); 并将您的html更改为: <a class="button">OK</a> $(文档).ready(函数(){ $inputs=$('one,'tow'); $inputs.更改(检查); $submit=$(“#submit”); 函数检查(){ var结果=1; 对于(变量i=0;i<$inputs.length;i++){ 如果(!$inputs[i].值){ 结果=0; 打破 } } 如果(结果){ $submit.removeAttr('disabled'); }否则{ $submit.attr('disabled','disabled'); } } 检查(); });代码>如果遇到这种情况,这是我的实现。 首先,am使用以下样式在页面加载时将禁用的类添加到锚定标记上: .disabled { color : gray // gray out button color cursor : default; // make cursor to arrow // you can do whatever styling you want // even disabled behaviour } 我们使用jquery on document ready和keyup事件添加这些类,如下所示: $(function () { // add disabled class onto button class(anchor tag) $(".button").addClass('disabled'); // register keyup handler on one and two element $("#one, #two").keyup(function () { var one = $("#one").val(), two = $("#two").val(); // checking if both not empty, then remove class disabled if (one && two) $(".button").removeClass('disabled'); // if not then add back disabled class else $(".button").addClass('disabled'); }); // when we pressing those button $('.button').click(function (e) { // we check if those button has disabled class yet // just return false if ($(this).hasClass('disabled')) return false; }); }); 应该是two=$(“two”).val()?你搞定了!非常感谢。