简体   繁体   English

jQuery禁用搜索输入不起作用

[英]jQuery Disable Search Input Not working

I have a search input which does the following. 我有一个搜索输入,该输入执行以下操作。 If the search field is empty the button is disabled (this works fine) 如果搜索字段为空,则按钮被禁用(可以正常工作)

Problem is if the form loads with text inside the search filed, I still have to use a space or backspace or type in order to enable the submit button. 问题是,如果表单在搜索字段中加载了文本,我仍然必须使用空格或退格键或键入才能启用提交按钮。

I want the submit button to be enabled if there is value in the search Field when the form loads. 如果加载表单时搜索字段中有值,我希望启用提交按钮。

Here is a fiddle: http://jsfiddle.net/sz6aLjoz/ 这是一个小提琴: http : //jsfiddle.net/sz6aLjoz/

Form: 形成:

<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: 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);
        }
    });
});

Add a call to trigger .keyup() at the end of your code: 在代码末尾添加一个调用以触发.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 example jsFiddle示例

This way when the page loads, the event will be triggered as if the user had hit a key, and your code executed. 这样,在页面加载时,事件将被触发,就像用户按下了键一样,并且您的代码已执行。

It's because you started with the button disabled, you can remove the disabled attribute from the button and add this line inside your ready function 这是因为从禁用按钮开始,您可以从按钮中删除Disabled属性,并将此行添加到ready函数中

$('.enableOnInput').prop('disabled', $('#searchInput').val() == '');

Fiddle here 在这里摆弄

If you bind the input via JQuery, it will automatically detect any changes to the value of "#searchInput". 如果通过JQuery绑定输入,它将自动检测对“ #searchInput”值的任何更改。 Here's the JavaScript: 这是JavaScript:

$(function () {
    $("#searchInput").bind("change paste 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);
        }
    });
});

So now, if there the user has autofill on, or has previously search for something then double clicks on the input the selects a value, the code will check the value of "#searchInput" - so; 因此,现在,如果用户已经启用了自动填充功能,或者以前曾搜索过某些内容,然后双击输入,然后选择一个值,那么代码将检查“ #searchInput”的值-这样; if the users pastes, if there's a keyup event or if the value changes, the code inside of the bind() will be ran. 如果用户粘贴,如果有keyup事件或值更改,则将运行bind()内部的代码。

Here's the JSFiddle 这是JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM