我的asp.net mvc web应用程序中有以下代码: -
@using (Ajax.BeginForm("AdvanceSearchIndex","Home",
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
LoadingElementId = "progress",
UpdateTargetId = "SearchTable"
}))
{
<p><span class="f">IP Address <input placeholder="Search by IP " name="ip" type="text" /> </span></p>
<p><span class="f">MAC Address <input placeholder="Search by Mac " name="mac" type="text" /> </span></p>
<input class="btn btn-success" type="submit" value="search" />
<img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress" />
}
但是我需要编写一个javaScript代码,以便在单击“搜索”按钮时,IP地址或MAc地址不为空或空?
答案 0 :(得分:1)
我也在这里回答你:How to implement a Required validation to check if the user enter atleast one value
@using (Ajax.BeginForm("AdvanceSearchIndex","Home",
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
LoadingElementId = "progress",
UpdateTargetId = "SearchTable",
OnBegin = "validateForm"//note this line calling js function
}))
{
<p><span class="f">IP Address <input placeholder="Search by IP " name="ip" type="text" /> </span></p>
<p><span class="f">MAC Address <input placeholder="Search by Mac " name="mac" type="text" /> </span></p>
<input class="btn btn-success" type="submit" value="search" />
<img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress" />
}
<script>
function validateForm(){
//return true/false
return $("[name=ip]").val() !="" || $("[name=nac]").val()
}
</script>