我有一个搜索输入,它执行以下操作。 如果搜索字段为空,则该按钮被禁用(此工作正常)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