jQuery:当文本框有内容时使按钮可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1289660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:07:37  来源:igfitidea点击:

jQuery: Make a Button Visible When a TextBox Has Contents

jqueryeventstextboxbuttonvisibility

提问by Jim G.

Given: I have a textbox and a hidden button.

鉴于:我有一个文本框和一个隐藏按钮。

Wanted: When the textbox is neither null nor empty, show the button. When the textbox is null or empty, hide the button.

通缉:当文本框既不为空也不为空时,显示按钮。当文本框为空或为空时,隐藏按钮。

Question: How should I do this? Should I use jQuery and bind to the textbox's keyup event?

问题:我该怎么做?我应该使用 jQuery 并绑定到文本框的 keyup 事件吗?

回答by VoteyDisciple

Sure, the keyupevent sounds like a fine idea. You might do something like:

当然,这个keyup活动听起来是个好主意。您可能会执行以下操作:

$("textarea").keyup(function() {
  if ($(this).val().replace(/ /g, '') == '')
    $("#id-of-button").show();  
  else
    $("#id-of-button").hide();
});