简体   繁体   English

如何使输入字段为空时,按钮什么也不做?

[英]How can I make it so that when input field is empty, button does nothing?

I'm making a simple searcher in which the user enters a query, clicks search, and the program returns either the location of said element, or a "No results found" line. 我正在做一个简单的搜索器,其中用户输入查询,单击搜索,程序将返回所述元素的位置或“未找到结果”行。

I'm having trouble with the search button itself. 我在搜索按钮本身上遇到了麻烦。 It works perfectly fine when the element that is being searched exists, but if I click on it while the input is blank, it returns the "No results found" message. 当要搜索的元素存在时,它可以很好地工作,但是如果我在输入为空白时单击它,它将返回“未找到结果”消息。 I would like it so that it does nothing. 我想要它什么都不做。

I'm using mainly JavaScript. 我主要使用JavaScript。 What I've tried so far is make an if statement to check the length of the input, and then select the element from the DOM and make it disabled when length is 0. 到目前为止,我尝试过执行if语句来检查输入的长度,然后从DOM中选择元素,并在length为0时将其禁用。

I have tried adding both console.log and alert() to check the state of the button (enabled/disabled), and they both work equally, no matter the length of the input value. 我尝试添加console.logalert()来检查按钮的状态(启用/禁用),并且无论输入值的长度如何,它们都可以正常工作。

<button value="submit" id="button" data-key="13" onclick="clickFunction()">
</button>


function clickFunction() {
    var input = document.getElementById('input_id').value;
    var input = input.toLowerCase();

    /* disables button if there is no input */
    if ( input.length === 0 ) {
        document.getElementById("button").disabled = true;
        console.log("disabled");
    } else if (input.length > 0) {
        document.getElementById("button").disabled = false;
        console.log("enabled");
    }
}    

I have also tried using jQuery ($("#button").attr("disabled", true)) , but it's not working either. 我也尝试过使用jQuery ($("#button").attr("disabled", true)) ,但是它也不起作用。

Am I missing something? 我想念什么吗?

You need to stop the click, disabling the button with click means you will not be able to enable it. 您需要停止点击,并通过点击禁用按钮,这意味着您将无法启用它。

 function clickFunction(evt) { var input = document.getElementById('input_id').value.trim(); // if (!input.length) { evt.preventDefault(); } if (!input.length) return false; // cancel click return true } 
 <form> <input type="search" id="input_id" name="input_id" /> <button value="submit" id="button" data-key="13" onclick="clickFunction(event)"> </button> </form> 

but why use JavaScript, let HTML do it for you. 但是为什么要使用JavaScript,让HTML为您完成。

 <form> <input type="search" name="search" required /> <input type="submit" /> </form> 

You can modify your code in this manner: 您可以通过以下方式修改代码:

 function clickFunction() {
        var input = document.getElementById('input_id').value;
        input = input.toLowerCase();

        if (input.length) {
            // write your search logic here
        } else {
           // won't do anything
            return false;
        }
    }    

I hope it will help. 希望对您有所帮助。

this should work 这应该工作

 function validate(obj) { if (obj.value.length > 0) { document.getElementById("btnSave").disabled = false; } else { document.getElementById("btnSave").disabled = true; } } 
 <input type="text" id="txtName" onkeyup="validate(this)"/> <button disabled id="btnSave">save</button> 

暂无
暂无

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

相关问题 我怎样才能做到这一点,当用户输入一个字母时,输入会更改为下一个输入字段等等? - How can I make it so that when a user types a letter the input would change to the next input field and so on? 单击按钮后如何使输入字段为空? - how can i make the input field to be empty after i click a button? 如何在输入字段为空时启用按钮 - How to enable button when input field is not empty 当输入字段为空时如何禁用按钮? - How to disable a button when an input field is empty? 当用户单击按钮时,如何使其自动在文本字段中输入特定的关键字? - How can I make it automatically input particular keyword into text field when a user click a button? 我怎样才能让每次输出的项目在输入字段中输出,如果它被点击 - How can I make so that every time outputted-item is outputted in the input field if its clicked 如何创建输入字段的动态列表,当您写入时创建另一个输入字段,当输入为空时删除它? - How can I create a dynamic list of Input field which create another input field when you write and delete it when the input is empty? 为什么我的按钮有效,但是当我将其设为输入字段时却无效? - Why does my button work, but when I make it an input field it doesn't? jQuery在此字段中插入变量时,如何检查输入字段是否为空? - How can i check if an input field is empty or not when a variable is inserted in this field by jquery? 不使用表单进行验证如何禁用角度按钮如果<input>字段为空 - without using form for validation How can I disabled a button in angular if <input> field is empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM