2013-02-15 117 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插件,禁用按鈕,當一些輸入申請是空的。

相關問題