使用jQuery在HTML中显示错误消息

时间:2019-04-30 04:27:56

标签: jquery html

我正在尝试使用jQuery显示错误消息。出现错误时不显示。我已从另一个问题中对此进行了修改。

jQuery(包含在触发的if异常测试中)是:

$("#projectIDSelect").html("Please select a Project ID").addClass("error-msg"); // chained methods

html是:

<p>
    <label for="projectIDSelect">Project ID<span class="required">*</span></label>

    <!--Place to load the Project Names into for selection -->
    <select class="col-lg-3 col-md-3 col-sm-4 col-xs-6"  style="height:30px; color:black;" id="projectIDSelect" name="projectIDSelect" required>
        <option value="" disabled selected>Select a Project</option>
    </select>
</p>

此外,此命令运行后,下拉列表将不再包含选项(这些选项已动态加载)。

3 个答案:

答案 0 :(得分:2)

#projectIDSelect是一个组合框或下拉框。并且在您的jquery中,当您使用.html更新内容时,它不会显示错误,因为它不可能在下拉框中显示html内容。

您需要添加单独的标识符以显示消息。

您的代码可能是:

<p>
    <label for="projectIDSelect">Project ID<span class="required">*</span></label>

    <!--Place to load the Project Names into for selection -->
    <select class="col-lg-3 col-md-3 col-sm-4 col-xs-6"  style="height:30px; color:black;" id="projectIDSelect" name="projectIDSelect" required>
        <option value="" disabled selected>Select a Project</option>
    </select>
    <p id="projectIDSelectError"></p>
</p>

您的jquery可能是:

$("#projectIDSelectError").html("Please select a Project ID").addClass("error-msg"); // chained methods

答案 1 :(得分:0)

$(function() {
    if (!$('#projectIDSelect option:selected').val()) {
        $("#divError").html("Please select a Project ID").addClass("error-msg"); // chained methods
    } else {
        $("#divError").html("").removeClass("error-msg");
    }
    $("#projectIDSelect").change(function() {
        if (!$('option:selected').val()) {
            $("#divError").html("Please select a Project ID").addClass("error-msg"); // chained methods
        } else {
            $("#divError").html("").removeClass("error-msg");
        }
    });
});
.error-msg {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
   <label for="projectIDSelect">Project ID<span class="required">*</span></label>
   <!--Place to load the Project Names into for selection -->
   <select class="col-lg-3 col-md-3 col-sm-4 col-xs-6"  style="height:30px; color:black;" id="projectIDSelect" name="projectIDSelect" required>
      <option value=""  selected>Select a Project</option>
      <option value="1" >123</option>
      <option value="2" >321</option>
   </select>
</p>
<div id='divError'></div>

答案 2 :(得分:0)

您正在标签中选择id,而不是for选择器参考jQuery selector for the label of a checkbox,这会有所帮助

$("label[for='projectIDSelect']").html("Please select a Project ID").addClass("error-msg");
.error-msg
{
color:red
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<p>
    <label for="projectIDSelect">Project ID<span class="required">*</span></label>

    <!--Place to load the Project Names into for selection -->
    <select class="col-lg-3 col-md-3 col-sm-4 col-xs-6"  style="height:30px; color:black;" id="projectIDSelect" name="projectIDSelect" required>
        <option value="" disabled selected>Select a Project</option>
    </select>
</p>