【发布时间】:2021-04-02 23:14:41
【问题描述】:
每个周期都有一个 PHP,我从数据库中获取文章。在这些文章中,我们有一个带有表格的评论部分。在发送评论之前,我想用 jQuery 检查输入中是否有任何内容。
由于文章是通过 PHP 循环带来的,我只想检查正在写评论的文章,但 jQuery 会检查所有文章,并且只启用或禁用从数据库。我希望 jQuery 只检查带有书面评论的文章。
这就是我正在做的事情:
$(document).ready(function() {
$(".comment-submit").attr("disabled", true);
$("#group-post-comment-input").keyup(function() {
if ($(this).val().length != 0) {
$(".comment-submit").attr("disabled", false);
} else {
$(".comment-submit").attr("disabled", true);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="comment.php" method="POST">
<input autocomplete="off" type="text" placeholder="Add a comment" id="group-post-comment-input">
<button class="comment-submit">
Comment
</button>
</form>
<br>
<form action="comment.php" method="POST">
<input autocomplete="off" type="text" placeholder="Add a comment" id="group-post-comment-input">
<button class="comment-submit">
Comment
</button>
</form>
<br>
<form action="comment.php" method="POST">
<input autocomplete="off" type="text" placeholder="Add a comment" id="group-post-comment-input">
<button class="comment-submit">
Comment
</button>
</form>
正如您在上面的 sn-p 中看到的,这些按钮仅在仅在第一个输入上写入文本时才启用。我希望在将文本写入其依赖输入时启用按钮。如果输入 2 上有文本,则启用按钮 2,依此类推。
我该怎么做?
【问题讨论】:
-
仅适用于较旧的 jQuery 吗?不在 JavaScript 代码 (ES10) 中?
-
您的 HTML 代码错误,因为
id属性必须具有唯一值
标签: javascript html jquery