根据无线电输入验证输入字段并显示错误消息

时间:2017-09-13 19:31:44

标签: javascript jquery html asp.net-mvc razor

我正在尝试在提交表单时使用jQuery显示错误消息,当单选按钮被选为true且输入框为空时,由于某种原因它失败了。任何建议表示赞赏!

<div class="row col-lg-offset-2 top-buffer">
<h4>
    <strong>
        <span>Is Account Deleted?:</span>
    </strong>
</h4>
            @Html.RadioButtonFor(model => model.IsDeleted, true) @Html.Label("Yes")
            @Html.RadioButtonFor(model => model.IsDeleted, false) @Html.Label("No")

<br>
</div>
<div class="row col-lg-offset-2 top-buffer" id="reason">
    <div class="col-sm-2">
        <b>Reason for Delete:</b>
    </div>
    <div class="col-sm-10">
        @Html.TextBoxFor(model => model.reason, new { @class = "form-control", @id = "reason" })

        <br />
    </div>
    <div class="row col-sm-12">
        <label id="lblErrorMsg" class="has-error" style="color: #BD362F"></label>
    </div>
    <div class="col-sm-4">
        <button type = "submit" id="Next" name="Next" class="btn btn-primary">Next</button>
    </div>
</div>

脚本:

<script>
$("#Next").click(function () {
    if ($("input[name='IsDeleted']" === "True" && $("#reason").val(''))) {
        $("#lblErrorMsg").text("Please enter meter number");
    } else {
        $("#lblErrorMsg").text("");
    }
});

1 个答案:

答案 0 :(得分:0)

我无法确定,因为我没有看到完整的HTML代码,但请尝试代替:

if ($("input[name='IsDeleted']" === "True" && $("#reason").val(''))) {

这样:

if ($("input[name='IsDeleted']:checked").val() === "True" && $("#reason").val() == '')

改进了一些答案,但要确保IsDeleted单选按钮的值为“True”