@using (Html.BeginForm()
{
<p>/span>ID。@Html.TextBoxFor(a => a.user_id)</p>
<p>/span>Name: @Html.TextBoxFor(a => a.user_name)</p>
<input type="submit" />
}
這是我的代碼。當人們點擊提交按鈕時,我必須檢查空值。如果用戶輸入空值,Web將顯示警告。
我試著給hteml.textboxfor()添加id。
<p>ID。@Html.TextBoxFor(a => a.user_id, new {id= "user_id"})< /p>
我不確定這段代碼是否正確。請讓我知道。
uj5u.com熱心網友回復:
@Html.TextBoxFor將自動生成id user_id。
而且你可以像下面這樣做:
@using (Html.BeginForm()
{
<p>/span>ID。@Html.TextBoxFor(a => a.user_id)</p>
<p>/span>Name: @Html.TextBoxFor(a => a.user_name)</p>
< input type="submit" id="submit" />>
}
@section scripts{
<script>
$("#submit").on("click", function (e) {
var id = $("#user_id").val() 。
var name = $("#user_name").val()。
if (id == " || name == ") {
e.preventDefault()。
alert("value can't be null") 。
}
})
</script>
更新:
<script>
document.getElementById("submit"/span>)。 addEventListener("click", function (event) {
var id = document.getElementById("user_id"/span>).value。
var name = document.getElementById("user_name").value。
if (id == ""/span> || name == ""/span>) {
event.preventDefault()。
alert("value can't be null") 。
}
});
</script>>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/320349.html
標籤:
上一篇:在試圖激活'Student.Controllers.MyController'時,無法解決'Microsoft.AspNetCore.Mvc.ActionContext&
