jQuery禁用搜索输入不起作用

肖恩·阿斯兰

我有一个搜索输入,该输入执行以下操作。如果搜索字段为空,则该按钮被禁用(可以正常工作)

问题是,如果表单在搜索字段中加载了文本,我仍然必须使用空格或Backspace或键入才能启用“提交”按钮。

如果加载表单时搜索字段中有值,我希望启用提交按钮。

这是一个小提琴: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);
        }
    });
});
j08691

.keyup()在代码末尾添加调用以触发

$('#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);
    }
}).keyup();

jsFiddle示例

这样,在页面加载时,将触发事件,就像用户按下了键一样,并且您的代码已执行。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章