如果 textarea 包含此文本,则运行查询

If textarea contains this text then run query

提问人:The Destiny 提问时间:3/14/2023 最后编辑:Elikill58The Destiny 更新时间:3/14/2023 访问量:42

问:

这是我的代码:

$(document).ready(function () {
   if ($("#write_text_id").text() === "Saurav & Kunal Are God") {
      $(".submit_form_field").addClass('item-3');
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="test">
  <textarea id="write_text_id" class="textarea small" tabindex="105" placeholder="Write Saurav &amp; Kunal Are God" rows="4" cols="50"></textarea>
  <input id="submit_btn_id" class="submit_form_field" type="submit" value="Submit">
</div>

我期望如果我在文本区域中写“Saurav & Kunal Are God”,那么提交按钮将添加类。item-3

html jquery if语句

评论

0赞 Tushar 3/14/2023
textareaText.contains('Saurav & ...')

答:

0赞 Elikill58 3/14/2023 #1

您应该在输入输入时检查,而不是一次。为此,您可以使用jQuery,如此处所述使用。on('input', function)

当它出错时,我个人会删除该类,以便在文本再次更改时使其消失。

$(document).ready(function () {
    $("#write_text_id").on('input', function (e) {
        if (e.target.value === "Saurav & Kunal Are God") {
           $(".submit_form_field").addClass('item-3');
        } else
           $(".submit_form_field").removeClass('item-3');
    });
});
.item-3 {
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="test">
  <textarea id="write_text_id" class="textarea small" tabindex="105" placeholder="Write Saurav &amp; Kunal Are God" rows="4" cols="50"></textarea>
  <input id="submit_btn_id" class="submit_form_field" type="submit" value="Submit">
</div>

评论

0赞 Elikill58 3/14/2023
@TheDestiny 很高兴:)你能接受我左上角的复选标记吗?:)
0赞 Mitali Patel 3/14/2023 #2

   $('#write_text_id').on('keyup',function(){
        if ($('#write_text_id').val() === "Saurav & Kunal Are God") {
        
          $(".submit_form_field").addClass('item-3');
        }
        else
        {
         $(".submit_form_field").removeClass('item-3');
        }
    });
.item-3 {
  background-color: green;
}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="test">
    <textarea id="write_text_id" class="textarea small" tabindex="105" placeholder="Write Saurav &amp; Kunal Are God" rows="4" cols="50"></textarea>
    <input id="submit_btn_id" class="submit_form_field" type="submit" value="Submit">
    </div>

1赞 Miss Skooter 3/14/2023 #3

你要找的是而不是监视文本框的更改。EventListenerdocument.ready

请注意,在最初加载文档时,我添加的文档只会调用一次:document.readyconsole.log

$(document).ready(function() {
  console.log("test");
  if ($("#write_text_id").text() === "Saurav & Kunal Are God") {
    $(".submit_form_field").addClass('item-3');
  }
});
<script src="https://code.jquery.com/jquery-3.6.4.slim.min.js" integrity="sha256-a2yjHM4jnF9f54xUQakjZGaqYs/V1CYvWpoqZzC2/Bw=" crossorigin="anonymous"></script>
<div class="test">
  <textarea id="write_text_id" class="textarea small" tabindex="105" placeholder="Write Saurav &amp; Kunal Are God" rows="4" cols="50"></textarea>
  <input id="submit_btn_id" class="submit_form_field" type="submit" value="Submit">
</div>

但是,在使用 an 时,单击提交按钮时,您将获得所需的结果:EventListener

$("#write_text_id").on("change", (event) => {
  if ($("#write_text_id").val() === "Saurav & Kunal Are God") {
    console.log("in if condition");
    $(".submit_form_field").addClass('item-3');
  }
});
.item-3 {
  background-color: green
}
<script src="https://code.jquery.com/jquery-3.6.4.slim.min.js" integrity="sha256-a2yjHM4jnF9f54xUQakjZGaqYs/V1CYvWpoqZzC2/Bw=" crossorigin="anonymous"></script>
<div class="test">
  <textarea id="write_text_id" class="textarea small" tabindex="105" placeholder="Write Saurav &amp; Kunal Are God" rows="4" cols="50"></textarea>
  <input id="submit_btn_id" class="submit_form_field" type="submit" value="Submit">
</div>

还值得注意的是,我使用了,因为没有返回任何东西。$("#write_text_id").val().text()