[英]A button should show/hide depending on whether or not there is value in input field
Right now the button shows when input is added to the input field, but if I remove the input the button doesn't hide. 现在,当将输入添加到输入字段时,该按钮会显示,但是如果删除输入,该按钮不会隐藏。
My only idea for a solution is to divide the function into 2 seperate ones. 我唯一的解决方案是将函数分为2个单独的函数。 That is, one that adds the input to the input field on click, and then another functions that keeps track of input.val, and controls the hide/show effect of the button.
就是说,一个在单击时将输入添加到输入字段,然后是另一个功能,用于跟踪input.val并控制按钮的隐藏/显示效果。
Would that be a better way of doing it? 这样做会更好吗?
$("#peopleInPopup").on('click', '.list-group-item', function() {
var peopleName = $(this).children("span").text();
var peopleID = $(this).children("span").attr("class");
var input = $("#friendsNames");
input.val(input.val() + peopleName + "");
if (input.val().length === 0) {
$("#checkButton").toggle(false);
console.log("button should NOT display");
} else {
console.log("button should display");
$("#checkButton").toggle(true);
}
$("#checkButton").click(function() {
var newParticipants = input.val();
socket.emit("addParticipantsToConversation", newParticipants);
$("#chatToInfo").append(", ", input.val());
$("#friendsNames").val("");
$(":mobile-pagecontainer").pagecontainer("change", $("#pagefour"), {
transition: "slidedown"
});
});
});
Just use show and hide from jQuery instead, perhaps? 只是使用显示和隐藏来代替jQuery吧? I assume you want this to happen as the input is changing, so I've thrown in keyup (could use onChange or alternatives).
我假设您希望在输入更改时发生这种情况,所以我抛出了keyup(可以使用onChange或替代方法)。
Tweaking your code slightly... 稍微调整代码...
$('input').on('keyup', function (event) {
let value = event.target.value;
if (value && value !== '' && value.length > 0) {
$('#myButton').show();
} else {
$('#myButton').hide();
}
})
With a markup... 加上标记...
<input id='input' />
<button id='myButton'>GO</button>
And some sort of base style... 还有某种基本风格...
#go {
display: none;
}
Do the trick? 绝招?
I actually just found a solution to my problem. 我实际上只是找到了解决我问题的方法。 The code below makes it work:
下面的代码使其工作:
$("#peopleInPopup").on('click', '.list-group-item', function(){ var peopleName = $(this).children("span").text(); var peopleID = $(this).children("span").attr("class"); var input = $("#friendsNames"); input.val(input.val() + peopleName + ""); $("#checkButton").toggle(true); $("#friendsNames").on('input', function(event) { if (this.value.length === 0) { console.log("Works!"); $("#checkButton").toggle(false); console.log("button should NOT display"); } else { console.log("button should display"); $("#checkButton").toggle(true); } }); $("#checkButton").click(function(){ var newParticipants = input.val(); socket.emit("addParticipantsToConversation", newParticipants); $("#chatToInfo").append(", ",input.val()); $("#friendsNames").val(""); $(":mobile-pagecontainer").pagecontainer("change", $("#pagefour"), { transition: "slidedown" }); }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.