[英]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.log
和alert()
来检查按钮的状态(启用/禁用),并且无论输入值的长度如何,它们都可以正常工作。
<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.