我試圖在提交表單時顯示錯誤消息,當單選按鈕被選中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("");
}
});
嘗試TRUE;不正確的,也就是你的理由,但實際上建設? – clearshot66
使用條件驗證屬性(請參閱您的[上一個問題]的評論(https://stackoverflow.com/questions/46181191/hide-display-a-input-field-based-on-value-from-htmlradiobuttonfor-and -make-it-ma)) –
@StephenMuecke感謝您的鏈接,這真的很有幫助。但我試圖在視圖中執行此驗證。哪一個更好?這種方法也適用。 –