如果输入不为空则显示按钮

如果输入不为空则显示按钮

问题描述:

我不是 JavaScript 专家,所以我需要简单代码方面的帮助.我有一个按钮可以清除输入字段的值.

I am not much of a JavaScript guru, so I would need help with a simple code. I have a button that clears the value of an input field.

如果输入字段为空,我希望它(按钮)被隐藏,反之亦然(如果输入字段中有文本则可见).

I would like it (the button) to be hidden if input field is empty and vice versa (visible if there is text inside the input field).

解决方案可以是纯 JavaScript 或 jQuery,没关系.越简单越好.

The solution can be pure JavaScript or jQuery, it doesn't matter. The simpler, the better.

if(!$('input').val()){
    $('#button').hide();
}
else {
    $('#button').show();
}

最简单的形式;)