@using (Html.BeginForm())
{
<p>ID: @Html.TextBoxFor(a => a.user_id)</p>
<p>Name: @Html.TextBoxFor(a => a.user_name)</p>
<input type="submit" />
}
這是我的代碼。當人們單擊提交按鈕時,我必須檢查空值。如果人員輸入 null,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>ID: @Html.TextBoxFor(a => a.user_id)</p>
<p>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").addEventListener("click", function (event) {
var id = document.getElementById("user_id").value;
var name = document.getElementById("user_name").value;
if (id == "" || name == "") {
event.preventDefault();
alert("value can't be null");
}
});
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415897.html
標籤:
