使用 jQuery 检查文本可见性 .包含代码的行为不符合预期

checking text visibility with jquery .contains code not behaving as expected

本文关键字:不符合 代码 检查 jQuery 文本 可见性 包含 使用      更新时间:2023-09-26

我有一个表单,每当单击提交按钮时必填字段留空时,该表单就会动态显示错误消息"这是必填问题"。 我想看看该错误消息是否在网页上可见。如果是这样,我应该显示一个警告框,上面写着"在表单中发现错误,请更正它们!如果没有,那么我可以继续处理表格。我为此使用 jquery。问题是:我填写了表格,没有任何错误,当我单击提交按钮时,它仍然弹出警报框。我的代码正确吗?

if ($('div:contains(This is a required question)').is(':visible')) {
     alert('Found errors in the form, please correct them!');
} else {
  google.script.run.withSuccessHandler(Success).withFailureHandler(failed).processForm();
}
}

:contains()接受字符串,则需要用引号传递它。

所以使用,

 $('div:contains("This is a required question")') 

完整代码

if ($('div:contains("This is a required question")').is(':visible')) {
    alert('Found errors in the form, please correct them!');
} else {
    google.script.run.withSuccessHandler(Success).withFailureHandler(failed).processForm();
}