我有一個搜索輸入,它執行以下操作。 如果搜索字段爲空,則該按鈕被禁用(此工作正常)jQuery禁用搜索輸入不起作用
問題是如果表單在搜索字段中加載了文本,我仍然必須使用空格或退格或類型以啓用提交按鈕。
如果表單加載時搜索字段中存在值,我希望啓用提交按鈕。
這裏是一個小提琴:http://jsfiddle.net/sz6aLjoz/
形式:
<form>
<input type="text" name="dosearch" id="searchInput" value="test" class="form-control" placeholder="Search...">
<button name="submit" class="enableOnInput" id="submitBtn" disabled='disabled' type="submit">Search</button>
</form>
JQuery的:
$(function() {
$('#searchInput').keyup(function() {
if ($(this).val() == '') { //Check to see if there is any text entered
//If there is no text within the input ten disable the button
$('.enableOnInput').prop('disabled', true);
} else {
//If there is text in the input, then enable the button
$('.enableOnInput').prop('disabled', false);
}
});
});
啊..感謝奏效..:d ...這就是我的回答 – user2912702 2014-08-27 15:29:16
哇,這是有趣的。你能解釋爲什麼這個工作嗎? – nothing9 2014-08-27 15:32:01
@ nothing9 - 我們在這裏所做的只是在末尾添加'.keyup()',它只是觸發鍵盤事件。這相當於添加'.trigger('keyup')'。 – j08691 2014-08-27 15:37:42