javascript - 有什么方法可以防止在 MVC 5 中提交表单?

标签 javascript jquery asp.net-mvc forms

我有一个来自。

@using (Html.BeginForm())
{
 // From content 
 <div class="form-group btn-group">
                <button type="submit" class="btnPrimaryStyle btn  col-sm-3 col-xs-12 pull-left">Save</button>
            </div>
}

一切正常。我有一个不合时宜的 dropdwon。我想检查一下,如果选择了下拉值,则可以提交表单,否则不会提交。简而言之,下拉选择是必需的,而且是表格外的。我可以使用 jquery 或 javascript 检查是否选择了 dropdwon 天气的值。我的问题是,如果未选择值,我如何阻止提交表单?

最佳答案

使用 jQuery 很简单:

$("#your-form-id").submit(function (e) { // note that it's better to use form Id to select form
      if($("#YourDropDownID").val() == 0 || $("#YourDropDownID").val() == '') // here you check your drop down selected value
      {
         e.preventDefault(); // here you stop submitting form
      }
}

要将 id 设置为您的 form 使用此重载:

Html.BeginForm(null, null, FormMethod.Post, new { id = "your_form_id" })

关于javascript - 有什么方法可以防止在 MVC 5 中提交表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37022094/

相关文章:

javascript - 图例中的条形标签

javascript - Angular + Laravel : loading view via ngRoute

c# - CultureInfo 的实例(来自同一文化)根据操作系统而变化

asp.net-mvc - ASP.NET MVC : How to disable SSL 2. 0

asp.net-mvc - 如果 ModelState 表明字段无效,则清除字段值

javascript - 如何在数组中推送多个值?

用于检查字段是否为空的 Javascript 函数

jquery - 将鼠标悬停在图像上时显示文本

jquery - Highchart - 线条或区域不从左侧点开始

jquery - 了解 jQuery 中的优先级