2013-02-15 98 views
0

我有一个输入文本字段和一个按钮。当文本字段为空时,该按钮应该禁用。当字段被填充时,该按钮应该被启用。我试着用下面的代码,但没有正常工作帮助我!尝试禁用输入文本字段为空时的按钮

$(document).ready(function(){ 
$("#wmclrsrch").attr("disabled", "disabled"); 

if ($('#search_number').is(":empty")) { 
    alert ("verdadeiro"); 
    $("#wmclrsrch").attr("disabled", true); 
} 
$('#wmsearch').click(function(){ 
    $("#wmclrsrch").attr("disabled", false); 
    $("#wmclrsrch").attr("enabled", true); 
}); 

});

的HTML是:使用keyup事件

<form id="form"> 
    <label for="search_number">Mobile Number:</label> 
    <input id="search_number" type="text" /> 
    <button id="wmclrsrch">Clear Search</button> 
</form> 

由于事先

回答

0

检查输入的文字:

$('#search_number').keyup(function(){ 
    if($(this).val() != '') 
    { 
     //Eneable your button here 
    } 
    else 
    { 
     //Disable your button here 
    } 
}); 
1

你可以听的keyup事件并使用prop方法。

$(document).ready(function(){ 
    $('#search_number').keyup(function(){ 
     $("#wmclrsrch").prop("disabled", !this.value.length);  
    }).keyup(); 
}); 
0

你可以试试下面的脚本:

<script> 
/* 
* 
* A tiny jQuery plugin that could control the button's state 
* 
* code from https://github.com/jvyyuie/snbutton 
* 
*/ 
var SNButton = { 
    init: function(e) { 
     var snnode = "#"+$("#"+e).data("snnode"); 
     $("#"+e).attr("disabled", ""==$(snnode).val()); 
     $(snnode).on('input propertychange', function() { 
      $("#"+e).attr("disabled", ""==$(snnode).val()); 
     }); 
    } 
} 
</script> 

<input id="textinput" /> 
<button id="btn" data-snnode="textinput">Button</button> 

<script> 
SNButton.init("btn"); 
</script> 

这是一个非常简单的jQuery插件,禁用按钮,当一些输入申请是空的。

相关问题